September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

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

  • July 9, 2002
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Two parameters required

This version of the newInstance method requires two parameters.  The first parameter specifies the component type.  This must be a reference to a Class object representing the component type of the new array object.

The second parameter, of type int, specifies the length of the new array object.

The Class object

The second parameter that specifies the array length is fairly obvious.  However, you may need some help with the first parameter.  Here is part of what Sun has to say about a Class object.

"Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects."
Getting a reference to a Class object

I know of three ways to get (or refer to) a Class object.

  • Class objects for primitive types
  • The getClass method
  • The forName method
Class objects for primitive types

There are nine predefined Class objects that represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent: boolean, byte, char, short, int, long, float, and double.  You can refer to these class objects using the following syntax: boolean.class, int.class, etc.  I will illustrate this later in this lesson.

The getClass method

If you have a reference to a target object (ordinary object or array object), you can gain access to a Class object representing the class from which that object was instantiated by invoking the getClass method of the Object class, on that object.  The getClass method returns a reference of type Class that refers to a Class object representing the class from which the target object was instantiated.

The forName method

The static forName method of the Class class accepts the name of a class or interface as an incoming String parameter, and returns the Class object associated with the class or interface having the given string name.  (The forName method cannot be used with primitive types as a parameter.)

Class object for the String class

Referring back to Listing 1, you will see that the first parameter passed to the newInstance method was a reference to a Class object representing the String class.  Thus, the statement in Listing 1 creates a one-dimensional array object, of component type String, three elements in length.  The reference to the array object is saved in the generic reference variable of type Object.

(In case you haven't recognized it already, this is an alternative to syntax such as new String[3].  Note that there are no square brackets in this alternative approach.  Thus, it might be said that this approach is more mainstream OOP than the approach that requires the use of square brackets.)
Populate the array object

The code in Listing 2 uses two static methods of the Array class to populate the three elements of the array object with references to objects of type String.
 

    for(int i = 0; i < 
             Array.getLength(v1); i++){
      Array.set(v1, i, "a"+i);
    }//end for loop

Listing 2

The getLength method

The getLength method is used in Listing 2 to get the length of the array for use in the conditional expression of a for loop.  Note that unlike the sample programs in the previous lesson (that stored the array object's reference as type Object), it was not necessary to cast the reference to type String[] in order to get the length.

The set method

The set method is used in Listing 2 to store references to String objects in the elements of the array object.  Again, unlike the programs in the previous lesson, it was not necessary to cast the array reference to type String[] to access the elements.  In fact, there are no square brackets anywhere in Listing 2.

Display the data

Listing 3 uses a similar for loop to display the contents of the String objects whose references are stored in the elements of the array object.
 

    for(int i = 0; i < 
             Array.getLength(v1); i++){
      System.out.print(
               Array.get(v1, i) + " ");
    }//end for loop

Listing 3

No square brackets

Once again, note that no casts, and no square brackets were required.  In fact, this approach makes it possible to deal with one-dimensional array objects using a syntax that is completely devoid of square brackets.  Rather than using square brackets to access array elements, this is a method-oriented approach to the use of array objects.  This makes it possible to treat array objects much the same as we treat ordinary objects in Java.

A two-dimensional rectangular array object tree

Next, I will use the methods of the Array class to create, populate, and display a rectangular two-dimensional array object tree, whose elements contain references to objects of the class String.

Another overloaded version of newInstance

To accomplish this, I will use the other overloaded version of the newInstance method.  This is the version that requires a reference to an array object of type int as the second parameter.

(Note that the Sun documentation describes two different behaviors for this method, depending on the whether the first parameter represents a non-array class or interface, or represents an array type.  This sample program illustrates the first possibility.)
The second parameter

As mentioned above, the version of the newInstance method that I am going to use requires a reference to an array object of type int as the second parameter.

(The length of the array object of type int specifies the number of dimensions of the multi-dimensional array object.  The contents of the elements of the array object of type int specify the sizes of those dimensions.)
Thus, my first task is to create and populate an array object of type int.

An array object of type int

Listing 4 shows the code required to create and populate the array object of type int.  This is a one-dimensional array object having two elements (length equals 2).  The first element is populated with the int value 2 and the second element is populated with the int value 3.
 

    Object v2 = Array.newInstance(
                         int.class, 2);
    Array.setInt(v2, 0, 2);
    Array.setInt(v2, 1, 3);

Listing 4

Why do we need this array object?

When this array object is used later, in conjunction with the version of the newInstance method that requires a reference to an array object of type int as the second parameter, this array object will specify an array object having two dimensions (a rectangular array). The rectangular array will have two rows and three columns.

Same newInstance method as before
(Note that Listing 4 uses the same version of the newInstance method that was used to create the one-dimensional array object in Listing l.)
Class object representing int

Note the syntax of the first parameter passed to the newInstance method in Listing 4.  As mentioned earlier, this is a reference to the predefined Class object that represents the primitive type int.  This causes the component type of the array object to be type int.

The setInt method

You should also note the use of the setInt method of the Array class to populate each of the two elements in the array in Listing 4 (with int values of 2 and 3 respectively).

The two-dimensional array object tree

Listing 5 uses the other overloaded version of the newInstance method to create a two-dimensional array object tree, having two rows and three columns.
 

    Object v3 = Array.newInstance(
       Class.forName(
       "java.lang.String"), (int[])v2);

Listing 5




Page 2 of 4



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel