November 26, 2014
Hot Topics:

The Essence of OOP using Java, Array Objects, Part 2

  • June 5, 2002
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

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.

Create only the root array object

However, the boldface code in Listing 13 creates only the array object at the root of the tree.  That array object is an array object 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 boldface portions of the statements in Listing 14 create two array objects of element type Object.
 

    v2[0] = new Object[3];
    v2[1] = new Object[3];

Listing 14

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 were to 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 of the two leaf array objects need not be the same.  Although I caused the length of the leaf objects to be the same in this case, I could just as easily have caused them to be 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 lesson, 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 the previous 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 two approaches.

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 array objects 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 leaves of the tree can be any length, but I will make them the same length in order to emulate a traditional rectangular two-dimensional array.

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.
 

    Object[] v3 = new Object[2];
Listing 15

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 shown in Italics (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 considerably different 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:

  • References to any object instantiated from any class
  • References to array objects whose elements are of any type (primitive or reference)
  • A mixture of the two kinds of references.
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 better understanding of Java object-oriented programming concepts than the programmer who uses the two approaches discussed earlier in this lesson.

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 in other languages that a programmer with little understanding of Java object-oriented programming concepts can probably muddle through the syntax based on prior knowledge.  However, it is unlikely that a programmer could muddle through this approach without really understanding what he 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 sample program 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 a reference to any object that is assignment compatible with the Object type.

(Assignment compatibility includes a reference to any object instantiated from any class, or a reference to any array object of any type (including primitive types), or a mixture of the two.)
    v3[0] = new Object[3];
    v3[1] = new Object[3];

Listing 16

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 array objects 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 the outer and inner loops.)
 

    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

Listing 17




Page 5 of 8



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel