<< Chapter < Page Chapter >> Page >

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 elementsof 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.

Listing 4 . An array object of type int.
Object v2 = Array.newInstance(int.class,2); Array.setInt(v2, 0, 2);Array.setInt(v2, 1, 3);

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 threecolumns.

Listing 5 . The two-dimensional array object tree.
Object v3 = Array.newInstance(Class.forName("java.lang.String"), (int[])v2);

A reference to the array object at the root of the tree is stored in the reference variable of type Object named v3 . Note that the tree is designed to store references to objects of type String .

(The number of dimensions and the size of each dimension are specified by the reference to the array object of type int passed as the second parameter.)

Square-bracket cast is required here

The required type of the second parameter for this version of the newInstance method is int[] . Therefore, there was no way for me to avoid the use of square brackets. I could either store the reference to thearray object as type Object and cast it before passing it to the method, (which I did), or save it originally as type int[] , (which I didn't). Either way, I would have to know about the type int[] .

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