<< Chapter < Page Chapter >> Page >

Populate the leaf elements

The nested for loop in Listing 6 uses the various methods of the Array class to populate the elements in the leaf array objects with references to objects of the class String .

Listing 6 . Populate the leaf elements.
for(int i=0;i<Array.getLength(v3);i++){ for(int j=0;j<Array.getLength(Array.get(v3,i));j++){ Array.set(Array.get(v3,i),j,"b" + (i+1)*(j+1));}//end inner loop }//end outer loop

Admittedly, the code in Listing 6 is a little complex. However, there is really nothing new there, so I won't discuss itfurther.

Display the data

Similarly, the code in Listing 7 uses the methods of the Array class in a nested for loop to get and display the contents of the String objects whose references are stored in the elements of the leaf array objects.Again, there is nothing new here, so I won't discuss this code further.

Listing 7 . Display the data.
for(int i=0;i<Array.getLength(v3);i++){ for(int j=0;j<Array.getLength(Array.get(v3,i));j++){ System.out.print(Array.get(Array.get(v3,i),j) + " ");}//end inner loop System.out.println();}//end outer loop System.out.println();

Very few square brackets

I will point out that with the exception of the requirement to create and pass an array object as type int[] , it was possible to write this entire example without the use of square brackets. This further illustrates the factthat the Array class makes it possible to create and work with array objects in a method-oriented manner, almost devoid of the use of square-bracket notation.

Sorting and Searching

Many college professors require their students to spend large amounts of time reinventing algorithms for sorting and searching (and for various collections and data structures as well). There was probably a time in history when that was an appropriate use of a student's time. However, in my opinion, that timehas passed.

Reuse, don't reinvent

Through a combination of the Arrays class, and the Java Collections Framework , most of the sorting, searching, data structures, and collection needs that you might have are readily available without a requirement for you toreinvent them.

(One of the most important concepts in OOP is reuse, don't reinvent .)

I will now illustrate sorting and searching using static methods of the Arrays class.

(Note that the Arrays class is different from the Array class discussed earlier.)

Create, populate, and display an array object

To give us something to work with, Listing 8 creates, populates, and displays the contents of an array object. Note that thearray object is populated with references to String objects. There is nothing new here, so I won't discuss the code in Listing 8 in detail.

Listing 8 . Create, populate, and display an array object.
Object v4 = Array.newInstance(Class.forName("java.lang.String"), 8);//Populate the array object. // Create a gap in the data.for(int i = 0; i<Array.getLength(v4); i++){ if(i<4){ Array.set(v4,i,"c"+(8-i));}else{ Array.set(v4,i,"c"+(18-i));}}//end for loop //Display the raw datafor(int i = 0; i<Array.getLength(v4); i++){ System.out.print(Array.get(v4,i)+ " ");}//end for 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