<< Chapter < Page | Chapter >> Page > |
(Assignment compatibility includes a reference to any object instantiated from any class, or a reference to any array object of any type (includingprimitive types), or a mixture of the two.)
Listing 16 . Populate the root object. |
---|
v3[0] = new Object[3];
v3[1]= new Object[3]; |
References to the two new array objects are stored in the elements of the array object that forms the root of the tree structure. The two new arrayobjects form the leaves of the tree structure.
Populate the leaf array objects
As in the previous two cases, the code in Listing 17 uses nested for loops to populate the array elements in the leaf objects with references to new Integer objects. (The Integer objects encapsulate int values based on the loop counter values for theouter and inner loops.)
Listing 17 . Populate the leaf array objects. |
---|
for(int i=0;i<v3.length;i++){
for(int j=0;j<((Object[])v3[i]).length;j++){
((Object[])v3[i])[j]= new Integer((i+1)*(j+1));
}//end inner loop}//end outer loop |
Added complexity
The added complexity of this approach manifests itself in
Inside and outside the parentheses
Note that within the inner loop, one of the square-bracket accessor expressions is inside the parentheses and the other is outside the parentheses.
Why are the casts necessary?
The casts are necessary to convert the references retrieved from the array elements from type Object to type Object[] . For example, the reference stored in v3[i] is stored as type Object .
Get length of leaf array object
The cast in the following expression converts that reference to type Object[] before attempting to get the value of length belonging to the array object whose reference is stored there.
((Object[])v3[i]).length
Assign a value to an element in the leaf array object
Similarly, the following expression converts the reference stored in v3[i] from type Object to type Object[] . Having made the conversion, it then accesses the jth element of the array object whose reference is stored there (in order to assign a value to that element).
((Object[])v3[i])[j]=
Display data in leaf array objects
Listing 18 uses similar casts to get and display the values encapsulated in the Integer objects whose references are stored in the elements of the leaf array objects.
Listing 18 . Display data in leaf array objects. |
---|
for(int i=0;i<v3.length;i++){
for(int j=0;j<((Object[])v3[i]).length;j++){
System.out.print(((Object[])v3[i])[j]+ " ");
}//end inner loopSystem.out.println();//new line
}//end outer loop |
The rectangular output
This approach produced the following output on the screen, (which is the same as the previous two approaches):
1 2 3
2 4 6
Ragged arrays
All the code in the previous three cases has been used to emulate a traditional rectangular two-dimensional array. In the first case, each row wasrequired to have the same number of elements by the syntax used to create the tree of array objects.
In the second and third cases, each row was not required to have the same number of elements, but they were programmed to have the same number of elementsin order to emulate a rectangular two-dimensional array.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?