<< Chapter < Page | Chapter >> Page > |
Listing 11 . Using length to populate the leaves of the tree structure. |
---|
for(int i=0;i<v1.length;i++){
for(int j=0;j<v1[i].length;j++){v1[i][j]=
new Integer((i+1)*(j+1));}//end inner loop
}//end outer loop |
Using length in loop's conditional expressions
Hopefully by now you can read and understand this code without a lot of help from me. I will point out, however, that the value returned by v1.length (in the conditional expression for the outer loop) is the number of leaves in the tree structure (this tree structure has two leaves).
I will also point out that the value returned by v1[i].length (in the conditional expression for the inner loop) is the number of elements in each leaf array object (each leaf object in this tree structure has three elements).
Finally, I will point out that the expression v1[i][j] accesses the jth element in the ith leaf, or sub-array. In the traditional sense of a rectangular array, this couldbe thought of as the jth column of the ith row. This mechanism is used to store object references in each element of each of the leaf array objects.
Populate with references to Integer objects
Thus, each element in each leaf array object is populated with a reference to an object of the type Integer . Each object of the type Integer encapsulates an int value calculated from the current values of the two loop counters.
Display leaf object contents
In a similar manner, the code in Listing 12 uses the length values in the conditional expressions of nested for loops to access the references stored in the elements of the leaf array objects, and touse those references to access and display the values encapsulated in the Integer objects whose references are stored in those elements.
Listing 12 . Display leaf object contents. |
---|
for(int i=0;i<v1.length;i++){
for(int j=0;j<v1[i].length;j++){System.out.print(
v1[i][j] + " ");}//end inner loop
System.out.println();//new line}//end outer loop |
The rectangular output
The code in Listing 12 produces the following output on the screen.
1 2 3
2 4 6
As you can see, this emulates a traditional two-dimensional array having two rows and three columns.
A ragged array with two rows and three columns
The second approach to emulating a traditional two-dimensional rectangular array will create a ragged array where each row is the same length.
(It is very important to note that, unlike this case, with a ragged array, the number of elements in each rowor the number of elements in each column can bedifferent.)
The most significant thing about this approach is the manner in which the tree of array objects is created (see Listing 13 ).
Listing 13 . Beginning of a ragged array with two rows and three columns. |
---|
Object[][]v2 = new Object[2][]; |
Three statements required
With this approach, three statements are required to replace one statement from the previous approach. (Two additional statements are shown in Listing 14 .)
A single statement in the previous approach ( Listing 10 ) created all three array objects required to construct the tree of array objects, and initialized the elements in the leaf array objects with null values.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?