<< Chapter < Page Chapter >> Page >

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

Listing 21 . A more general approach.
Object[] v5 = new Object[2]; v5[0]= new Object[2];v5[1] = new Object[3];

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 .

Listing 22 . Populate the leaf objects.
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

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 .

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 arraywhere 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 aone-dimensional array object of element type Object . I will populate the elements of that array object with references to other arrayobjects. 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 leafobjects 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 forthe 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 .

Listing 23 . Beginning of a more general case .
Object[] v6 = new Object[2]; v6[0]= new int[7];v6[1] = new int[3];

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 .

Listing 24 . Populate the leaf array elements.
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

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