<< Chapter < Page | Chapter >> Page > |
Create the array object
First consider the statement shown in Listing 15 . Compare this statement with the statements shown earlier in Listing 10 and Listing 13 .
Listing 15 . Create the array object. |
---|
Object[] v3 = new Object[2]; |
No double square brackets
Note in particular that the statement in Listing 15 does not make use of double square brackets, as was the case in Listing 10 and Listing 13 . Thus, the statement show in Listing 15 is entirely different from the statements shown in Listing 10 and Listing 13 .
Declare a reference variable
That portion of the statement to the left of the equal sign (=) declares a reference variable capable of storing a reference to an array object whose elements are capable of storing references of the type Object (not type Object[] as in the previous examples).
Refer to the root object
This reference variable will refer to an array object that forms the root of the tree structure. However, the root object in this case will be considerablydifferent from the root objects in the previous two cases.
In the previous two cases, the elements of the root object were required to store references of type Object[] (note the square brackets). In other words, an array object whose elements are of type Object[] can only store references to other array objects whose elements are of type Object .
A more general approach
However, an array object whose elements are of type Object (as is the case here), can store:
Therefore, this is a much more general, and much more powerful approach.
A price to pay
However, there is a price to pay for the increased generality and power. In particular, the programmer who uses this approach must have a much betterunderstanding of Java object-oriented programming concepts than the programmer who uses the two approaches discussed earlier in this module.
Particularly true relative to first approach
This is particularly true relative to the first approach discussed earlier. That approach is sufficiently similar to the use of multi-dimensional arrays inother languages that a programmer with little understanding of Java object-oriented programming concepts can probably muddle through the syntaxbased on prior knowledge. However, it is unlikely that a programmer could muddle through this approach without really understanding what she is doing.
Won't illustrate true power
Although this approach is very general and very powerful, this sample program won't attempt to illustrate that power and generality. Rather, this sampleprogram will use this approach to emulate a traditional two-dimensional rectangular array just like the first two approaches discussed earlier. (Later, I will also use this approach for a couple of ragged arrays.)
Populate the root object
The two statements in Listing 16 create two array objects, each having three elements. Each element is capable of storing areference to any object that is assignment compatible with the Object type.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?