December 18, 2014
Hot Topics:

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

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

Added complexity

The added complexity of this approach manifests itself in

  • The cast operators shown in boldface Italics in Listing 17
  • The attendant required grouping of terms within parentheses
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.
 

    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 loop
      System.out.println();//new line
    }//end outer loop

Listing 18

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 was required 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 elements in order to emulate a rectangular two-dimensional array.

A triangular array, sort of ...

Now I am going to show you some cases that take advantage of the ragged-array capability of Java array objects.  In the next case, (beginning with Listing 19), I will create a ragged array having two rows.  The first row will have two elements and the second row will have three elements.  (This array object might be thought of as being sort of triangular.)
 

    Object[][] v4 = new Object[2][];
    v4[0] = new Object[2];
    v4[1] = new Object[3];

Listing 19

You have seen this before

You saw code like this before in the second case discussed earlier.  However, in that case, the second and third statements created new array objects having the same length.  In this case, the second and third statements create array objects having different lengths.  This is one of the ways to create a ragged array in Java (you will see another way in the next case that I will discuss).

Populate the leaf array objects

Listing 20 populates the elements of the leaf array objects with references to objects of the class Integer.
 

    for(int i=0;i<v4.length;i++){
      for(int j=0;j<v4[i].length;j++){
        v4[i][j] = 
              new Integer((i+1)*(j+1));
      }//end inner loop
    }//end outer loop

Listing 20

You have seen this before also

You have also seen the code in Listing 20 before.  I repeated it here because this case clearly emphasizes the value of the length constant that is available in all Java array objects.  In the earlier case, the length of the two leaf array objects was the same, so it would have been feasible to simply hard-code that value into the conditional expression of the inner for loop.

The length is not the same now

However, in this case, the length of the two leaf array objects is not the same.  Therefore, it wouldn't work to hard-code a limit into the conditional expression of the inner for loop.  However, because the length of each leaf array object is available as a public member of the array object, that value can be used to control the number of iterations of the inner loop for each separate leaf array object.

The triangular output

The next section of code in the program shown in Listing 26 near the end of the lesson uses the same code as before to display the int values encapsulated in the Integer objects whose references are stored in the leaf array objects.  Since it is the same code as before, I won't repeat it here.

The output produced by this case is shown below:

1 2
2 4 6

Note that this is not the same as before, and this output does not describe a rectangular array. Rather, it describes a ragged array where the rows are of different lengths.

(As I indicated earlier, it is sort of triangular.  However, it could be any shape that you might want it to be.)
A more general approach

The next case, shown in Listing 21, is the same as the third case discussed earlier, except that the lengths of the leaf array objects are not the same.

As before, this case creates a one-dimensional array object of type Object (having two elements) that forms the root of a tree.  Each element in the root object contains a reference to another array object of type Object.

One of those leaf objects has two elements and the other has three elements, thus producing a ragged array (you could make the lengths of those objects anything that you want them to be).
 

    Object[] v5 = new Object[2];
    v5[0] = new Object[2];
    v5[1] = new Object[3];

Listing 21




Page 6 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