October 21, 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 »

Populate the leaf objects

As before, the elements in the leaf array objects are populated with references to objects of the class Integer, which encapsulate int values based on the current value of the loop counters.  This is shown in Listing 22.
 

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

Listing 22

Same code as before

This is the same code that you saw in Listing 17.  I repeated it here to emphasize the requirement for casting (shown in boldface in Listing 22).

Display the data

This case uses the same code as Listing 18 to display the int values encapsulated by the Integer objects whose references are stored in the elements of the leaf array objects.  I won't repeat that code here.

The triangular output

The output produced by this case is shown below:

1 2
2 4 6

Note that this is the same as the case immediately prior to this one.  Again, it does not describe a rectangular array. Rather, it describes a ragged array where the rows are of different lengths.

A more general case

I'm going to show you one more general case for a ragged array.  This case illustrates a more general approach.  In this case, I will create a one-dimensional array object of element type Object.  I will populate the elements of that array object with references to other array objects.  These array objects will be the leaves of the tree.

Leaf array objects are type int

In this case, the leaves won't be of element type Object.  Rather, the elements in the leaf objects will be designed to store primitive int values.

(An even more general case would be to populate the elements of the root object with references to a mixture of objects of class types, interface types, and array objects where the elements of the array objects are designed to store primitives of different types, and references of different types.  Note, however, each leaf array object must be designed to store a single type, but will accept for storage any type that is assignment-compatible with the specified type for the array object.)
This case begins in Listing 23, which creates the root array object, and populates its elements with references to leaf array objects of type int.
 
    Object[] v6 = new Object[2];
    v6[0] = new int[7];
    v6[1] = new int[3];

Listing 23

Leaf objects are different lengths

One of the leaf array objects has a length of 7.  The other has a length of 3.

Populate the leaf array elements

Listing 24 populates the elements in the leaf array objects with values of type int.
 

    for(int i=0;i<v6.length;i++){
      for(int j=0;
            j<((int[])v6[i]).length;
                                  j++){
        ((int[])v6[i])[j] = (i+2)*(j+2);
      }//end inner loop
    }//end outer loop

Listing 24

Similar to previous code

The code in Listing 24 is similar to code that you saw earlier.  The differences are:

  • Cast is to type int[] instead of object[] (see boldface code)
  • Values assigned are type int instead of references to Integer objects (see boldface Italic code)
Display the output

Finally, Listing 25 displays the int values stored in the elements of the leaf array objects.
 

    for(int i=0;i<v6.length;i++){
      for(int j=0;
            j<((int[])v6[i]).length;
                                  j++){
        System.out.print(
           ((int[])v6[i])[j] + " ");
      }//end inner loop
      System.out.println();//new line
    }//end outer loop

Listing 25

The code in Listing 25 is very similar to what you have seen before, and there should be no requirement for an explanation of this code.

The code in Listing 25 produces the following output:

4 6 8 10 12 14 16
6 9 12

I will leave it as an exercise for the student to correlate the output with the code.
 

Summary

When declaring a reference variable capable of referring to an array object, the array type is declared by writing the name of an element type followed by some number of empty pairs of square brackets [].

The components in an array object may refer to other array objects.  The number of bracket pairs used in the declaration of the reference variable indicates the depth of array nesting (in the sense that array elements can refer to other array objects).

An array's length is not part of its type or reference variable declaration.

Multi-dimensional arrays are not required to represent rectangles, cubes, etc.  They can be ragged.

The normal rules of type conversion and assignment compatibility apply when creating and populating array objects.

Object is the superclass of all other classes.  Therefore, an array of element type Object is capable of storing references to objects instantiated from any other class.  The type declaration for such an array object would be Object[].

An array of element type Object is also capable of storing references to any other array object.

If the declared element type for the array object is one of the primitive types, the elements of the array can be used to store values of any primitive type that is assignment compatible with the declared type (without the requirement for a cast).

If the declared element type is the name of a class, (which may or may not be abstract), a null reference or a reference to any object instantiated from the class or any subclass of the class may be stored in the array element.

If the declared element type is an interface type, a null reference or a reference to any object instantiated from any class that implements the interface can be stored in the array element.

A reference variable whose declared type is an array type does not contain an array.  Rather, it contains either null, or a reference to an array object.  Declaring the reference variable does not create an array, nor does it allocate any space for the array components.

It is possible to declare a reference to an array object and initialize it with a reference to an array object when it is declared.

A reference to an array object can refer to different array objects (of the same element type and different lengths) at different points in the execution of a program.

When declaring an array reference variable, the square brackets [] may appear as part of the type, or following the variable name, or both.

Once an array object is created, its type and length never changes.

An array object is created by an array creation expression or an array initializer.

An array creation expression (or an array initializer) specifies:

  • The element type
  • The number of levels of nested arrays
  • The length of the array for at least one of the levels of nesting
The length of the array is always available as a final instance variable named length.

An array element is accessed by an expression whose value is an array reference followed by an indexing expression enclosed by matching square brackets.

If an attempt is made to access the array with an invalid index value, an ArrayIndexOutOfBoundsException will be thrown.

Arrays must be indexed by integer values of the types int, short, byte, or char.  An array cannot be accessed using an index of type long.

If the elements in an array are not purposely initialized when the array is created, the array elements will be automatically initialized with default values.

The values in the array elements may be purposely initialized when the array object is created using a comma-separated list of expressions enclosed by matching curly braces.

The program in this lesson illustrated three different ways to emulate traditional rectangular two-dimensional arrays.

The program also illustrated two different ways to create and work with ragged arrays.

What's Next?

In the next lesson, I will provide so additional information about array objects, and then illustrate the use of the classes named Array and Arrays for the creation and manipulation of array objects.



Page 7 of 8



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel