<< Chapter < Page | Chapter >> Page > |
Create only the root array object
However, the code in Listing 13 creates only the array object at the root of the tree. That array object is an arrayobject having two elements capable of storing references of type Object[] .
Empty square brackets
If you compare this statement with the statement in Listing 10 , you will notice that the right-most pair of square brackets in Listing 13 is empty. Thus, Listing 13 creates only the array object at the root of the tree, and initializes the elements in that array object with null values.
Leaf array objects don't exist yet
The leaf array objects don't exist at the completion of execution of the statement in Listing 13 .
Create the leaf array objects
The statements in Listing 14 create two array objects of element type Object .
Listing 14 . Create the leaf array objects. |
---|
v2[0] = new Object[3];
v2[1]= new Object[3]; |
Save the references to the leaves
References to these two leaf objects are stored in the elements of the array object at the root of the tree, (which was created in Listing 13 ). Thus, these two array objects become the leaves of the tree structure of array objects.
This completes the construction of the tree structure. Each element in each leaf object is initialized with null .
Why bother?
You might ask why I would bother to use this approach, which requires three statements in place of only one statement in the previous approach.
The answer is that I wouldn't normally use this approach if my objective isto emulate a traditional rectangular array. However, this approach is somewhat more powerful than the previous approach.
The lengths of the leaf objects can differ
With this approach, the length values of the two leaf array objects need not be the same. Although I caused the length value of the leaf objects to be the same in this case, I could just as easily have caused them tobe different lengths (I will illustrate this capability later in the program).
Populate and display the data
If you examine the complete program in Listing 26 near the end of the module, you will see that nested for loops, along with the value of length was used to populate and display the contents of the leaf array objects. Since that portion of the code is the same as with theprevious approach, I won't show and discuss it here.
The rectangular output
This approach produced the following output on the screen, (which is the same as before):
1 2 3
2 4 6
Now for something really different
The next approach that I am going to show you for emulating a two-dimensional rectangular array is significantly different from either of the previous twoapproaches.
Not element type Object[]
In this approach, I will create a one-dimensional array object of element type Object (not element type Object[] ) . I will populate the elements of that array object with references to other arrayobjects of element type Object . In doing so, I will create a tree structure similar to those discussed above.
The length of the leaf objects
As with the second approach above, the array objects that make up the leavesof the tree can be any length, but I will make them the same length in order to emulate a traditional rectangular two-dimensional array.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?