<< Chapter < Page Chapter >> Page >

(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 thatthis 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 toobjects of type String .

Listing 2 . Populate the array object.
for(int i = 0; i<Array.getLength(v1);i++){ Array.set(v1, i, "a"+i);}//end for loop

The getLength method

The getLength method of the Array class 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 module (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 of the Array class 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 module, 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.

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

No square brackets

Once again, note that no casts, and no square brackets were required in Listing 3 . In fact, this approach makes it possible to deal with one-dimensional array objects using a syntax that is completely devoidof 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 elementscontain 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 version 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 whether the first parameter represents a non-arrayclass or interface, or represents an array type. This sample program illustrates the first possibility.)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask