<< Chapter < Page | Chapter >> Page > |
A triangular array, sort of ...
Now I am going to show you some cases that take advantage of the ragged-array capability of Java array objects. In the next case, (beginning with Listing 19 ), I will create a ragged array having two rows. The first row will have two elements and the second row will have threeelements. (This array object might be thought of as being sort of triangular.)
Listing 19 . A triangular array. |
---|
Object[][]v4 = new Object[2][];
v4[0]= new Object[2];v4[1] = new Object[3]; |
You have seen this before
You saw code like this in the second case discussed earlier. However, in that case, the second and third statements created new array objects having the samelength. In this case, the second and third statements create array objects having different lengths. This is one of the ways to create a ragged array inJava (you will see another way in the next case that I will discuss).
Populate the leaf array objects
Listing 20 populates the elements of the leaf array objects with references to objects of the class Integer .
Listing 20 . Populate the leaf array objects. |
---|
for(int i=0;i<v4.length;i++){
for(int j=0;j<v4[i].length;j++){v4[i][j]=
new Integer((i+1)*(j+1));}//end inner loop
}//end outer loop |
You have seen this before also
You have also seen the code in Listing 20 before. I repeated it here because this case clearly emphasizes the value of the length constant that is available in all Java array objects. In the earlier case, the length of the two leaf array objects was the same, so it would have been feasible to simply hard-code that value into the conditional expression ofthe inner for loop.
The length is not the same now
However, in this case, the length of the two leaf array objects is not the same. Therefore, it wouldn't work to hard-code a limit intothe conditional expression of the inner for loop. However, because the length of each leaf array object is available as a public member of the array object, that value can be used to control the numberof iterations of the inner loop for each separate leaf array object.
The triangular output
The next section of code in the program shown in Listing 26 near the end of the module uses the same code as before to display the int values encapsulated in the Integer objects whose references are stored in the leaf array objects. Since it is the same code as before, I won'trepeat it here.
The output produced by this case is shown below:
1 2
2 4 6
Note that this is not the same as before, and this output does not describe a rectangular array. Rather, it describes a ragged array where the rows are ofdifferent lengths.
(As I indicated earlier, it is sort of triangular. However, it could be any shape that you might want it to be.)
A more general approach
The next case, shown in Listing 21 , is the same as the third case discussed earlier, except that the lengths of the leaf array objects are not the same.
As before, this case creates a one-dimensional array object of type Object (having two elements) that forms the root of a tree. Each element in the root object contains a reference to another array object oftype Object .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?