October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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

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

A tree of empty array objects

The boldface code in Listing 10 (to the right of the equal sign, =) creates a tree structure of array objects.  The object at the root of the tree is an array object of type Object[], having two elements (a length of two).

The reference variable named v1 refers to the array object that forms the root of the tree.

Each of the two elements in the root array object is initialized with a reference to another array object.

(These two objects might be viewed as subarrays, or as child nodes in the tree structure).
Each of the child nodes is an array object of type Object, and has a length of three.

Each element in each of the two child node array objects is initialized to the value null (this is the default initialization for array elements of reference types that don't yet refer to an object).

Recap

To recap, the reference variable named v1 contains a reference to a two-element, one-dimensional array object.  Each element in that array object is capable of storing a reference of type Object[] (a reference to another one-dimensional array object of type Object).

Two subarray objects

Two such one-dimensional subarray (or child node) objects, of element type Object, are created.  References to the two subarray objects are stored in the elements of the two-element array object at the root of the tree.

Each of the subarray objects has three elements.  Each element is capable of storing a reference to an object as type Object.

The leaves of the tree
(These two subarray objects might be viewed as the leaves of the tree structure.)
Initialize elements to null

However, the objects of type Object don't exist yet.  Therefore, each element in each of the subarray objects is automatically initialized to null.

Arrays versus subarrays
(Note that there is no essential difference between an array object and a subarray object in the above discussion.  The use of the sub prefix is used to indicate that an ordinary array object belongs to another array object, because the reference to the subarray object is stored in an element of the owner object.)
Many dimensions are possible

Multi-dimensional arrays of any (reasonable) depth can be emulated in this manner.  An array object may contain references to other array objects, which may contain references to other array objects, and so on.

The leaves of the tree structure

Eventually, however, the elements of the leaves in the tree structure must be specified to contain either primitive values or references.  This is where the data is actually stored.

(Note however, that if the leaves are specified to contain references of type Object, they may contain references to other array objects of any type, and the actual data could be stored in those array objects.)
The length of an array

Every array object contains a public final instance variable named length, which contains an integer value specifying the number of elements in the array.

Once created, the length of the array encapsulated in an array object cannot change.  Therefore, the value of length specifies the length of the array throughout the lifetime of the array object.

Using length to populate the leaves of the tree structure

The value of length is very handy when processing array objects.  This is illustrated in Listing 11, which uses a nested for loop to populate the elements in the leaves of the tree structure referred to by v1(The elements in the leaf objects are populated with references to objects of type Integer.)
 

    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

Listing 11

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 subarray.  In the traditional sense of a rectangular array, this could be 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 to use those references to access and display the values encapsulated in the Integer objects whose references are stored in those elements.
 

    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

Listing 12

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.

(With a ragged array, the number of elements in each row can be different.)
The most significant thing about this approach is the manner in which the tree of array objects is created (see Listing 13).
 
    Object[][] v2 = new Object[2][];

Listing 13




Page 4 of 8



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel