We now know how to declare and initialize a fixed-size array. We'll have a look at the different classical manipulations that we can do on such arrays. First manipulation: printing an array. Let's imagine for example that we have declared and initialized an array t1 this way. We'll have the following situation in memory: In the variable t1, we have a reference, an address to the array. The array is initialized in this manner. Now let's suppose that I want to print the array by simply using an instruction line like this one. I pass as argument to System.out.println, the array t1 that I want to print. Here, I'm going to print the content of t1 which is a reference. This will translate into printing something a little strange which will look like this. Something like fe25235d, which will correspond to something that is a reference to an array, something not very understandable. In reality, what we want to do is printing the content of the array. To achieve this, we have no other choice than to provide a loop to iterate over each element of the array, and print them one by one. Let's suppose that we want to print each element of an array. Let's imagine that this array has initially been declared this way. It's an array of integers. How do we iterate over such an array to print each cell? Well, we've got two conventional solutions. The first is what is called "iteration over a set of values" and which will be written like this. The reserved word for and the array's values's type in parenthesis, which obviously has to be of the same type as what we specified here. We then have to give a variable name that will allow us to designate an array's cell. Then, after a colon, we must indicate the array over which we want to iterate. After that, to print the current cell, we just have to write this sort of instruction line. In fact, val will take each element of the array turn by turn and we'll print them in sequence one after the other with this iteration over the set of values. The even more standard solution is to use a for as we know it so far. A for that uses an index, which will go from 0 to the array's size. Here, this way of specifying the size isn't great because it is uncorrelated to the array itself. We have hard coded the value 3. The question of how to specify this size in a cleaner way will arise. We'll see that later. So we move on with our index to iterate over every cell of the array. We access the arrays' cells with the indexation mechanism. To sum up, if we wish to iterate over each value of an array we dispose of tools such as the iterations over a set of values. We have just seen a concrete example of it. We can also use a standard for iteration. At that moment, the problem of the computation of the array's size arises, which we'll see further. In reality there exists a third way of iterating over an array, the one that uses the notion of iterator. But this notion isn't presented in the context of this course. Let's come back to the question at hand concerning standard iterations: how can we specify the size of a fixed-size array? How can we recover this size? In Java, it is possible to get get the size of an array from it's name. To achieve this it is necessary to resort to a particular notation. This notation is the following: I use the array's name, followed by a point, followed by the reserved word 'length'. Here's a concrete example: I declare/initialize an array scores that contains four elements. If I want to print the array's size, I just have to resort to the notation I've just described. So the array's name, followed by a point, followed by 'length' which will, here, obviously, print 4. An analogous example here, with an array of booleans with two entries. It is important to note here that length will always give you the possible number of elements and that the effective number of filled cells isn't important. The fact that you explicitly put values or not isn't taken into account. Here's a concrete example: Here we declare an array without initializing it with concrete, effective values. Neither with an assignment of this type, nor with the assignment of a global array with a certain number of elements. I haven't put any explicit values in my array. However, if I print the array's size, I get 2 indeed. Why? Because here, when I construct the array, I allocate a size of 2 to it. Which means that I'll have this situation in memory. The variable score is a reference to an array with two entries. And we know that by default, these entries are initialzed with 0 for integers. So we are effectively dealing with a fixed-size array, containing two entries. length will give us this size. No matter whether we initialized it with explicit values or not. We were interested in how to iterate over the entire set of values of an array. We saw that it is possible to do it with a standard for-loop which lead us to discuss about the problem of the computation of the array's size. But before concluding the chapter on iterations let's come back to the other type of iteration, the for-loops allow us to iterate over a set of values. We saw that this kind of for-loops are written like this. We can easily see that it's a very simple and elegant way of iterating. We don't even have to worry about the array's size. However, there exists a certain number of limitations to this for-loop that we don't encounter with the standard iteration and which are the following. First limitation: an iteration on a set of values won't allow us to modify the content of the array. So here, if I have declared/initialized an array this way and that I want to iterate over this array with an iteration over a set of values. The fact of writing something like this, won't allow us to modify the content of the array. I can't modify the array by using a notation of this kind. Second limitation: I can iterate over one array at a time. So I can iterate over only one array. Here, a notation of this kind doesn't exist. If we want, for example, to iterate over two arrays at the same time, with such a notation. To compare the values of the first array with the values of the second array, this doesn't exist. We won't be able, with an iteration over a set of values to proceed to the simultaneous iteration of the two arrays. Third limitation: With the variable val, with the iteration over a set of values, I can only access one element at a time. For example, it won't be possible to compare an element of the array with the one following it. With val, I can access one element at a time. Finally, last limitation: I can iterate one step at a time only. For example, if I want to jump two by two in the array, this isn't possible with an iteration over a set of values. Note that the standard iterations don't have any of these limitations. It is possible to do everything with a standard for iteration. Manipulating arrays requires taking a number of precautions. You should pay attention to a number of aspects to not cause any errors. Errors either during the compilation, or during the program's execution. Errors we'll encounter while learning about arrays, are errors on indexes or access errors before the array's construction. There exists other types of errors which are related to the object-oriented programming aspect which will be presented in an upcoming course on object-oriented Java. Concerning the index problems, there are two things to be observed. The first is that the index of an array must imperatively be of int type. That's an integer. You must absolutely respect the array's bounds, which are between 0 and T-1, where T is the array's size. Here, I declare and construct an array of size 250. I commit a first error here, by trying to use an index that isn't of int type. So it's an error on the index type. Here I commit a second error by trying to use an index that is outside the array's bounds. So it's an error on the bounds. Likewise, an error on the bounds, here. We know that an array of 250 element has indexes going from 0 to 249. If I use the index 250, I overflow. I am not in the array's bounds. In this case, I also have a error on the bounds. Note that the first type of error is an error that will be detectable by the compiler, so when I compile the program. Contrariwise, the other errors are errors that will only be detected during the program's execution. Of course in practice, we won't commit this kind of error in such explicit ways. Often, it's if we get the indexes through a computation, and that our computation is wrong, that we'll end up with this kind of error. In general, we won't commit them in such explicit ways. Another classical error: the access before construction. In Java, if we want to access an array's element through indexation, it is imperative that the array has been constructed beforehand. An array's construction can be done, for example, by directly indicating the values when declaring the array. What can be seen here, with this instruction line. When I declare my array, I put a number of values into it, explicitly, with an assignment of this nature. So obviously, here, the element of index 0 really exists. It is possible to access it, it is entirely licit to proceed to this kind of access with the array's cell of index 0. Contrariwise, here, I have a slightly different situation. I declare the array but I don't construct the array, I don't assign initial values with an assignment of this nature to it, no more than I construct the array with this second type of instruction lines, which allows me to give an initial size to it and allocate a number of cells to this array. As I don't resort to this possibility, nor this possibility, nor this one, the access to a cell of the array is impossible. So here I would have an error. So what's missing with this instruction line is having built the array beforehand, which I can do with a trick of this nature. At that moment, the problem is resolved and I don't have problems of access before construction anymore.