<< Chapter < Page Chapter >> Page >

Complete program listing

A complete listing of the program is shown in Listing 12 below.

Listing 12 . Complete program listing .
/*File Array08.java Copyright 2002, R.G.BaldwinRev 2/10/02 This program illustrates the use ofstatic methods of the Array class to dynamically create and access Javaarrays. It also illustrates the use of staticmethods of the Arrays class to sort and search array objects.Tested using JDK 1.3 under Win 2000. **************************************/import java.lang.reflect.Array; import java.util.Arrays;public class Array08{ public static void main(String[] args){try{ //Create, populate, and display a// one-dimensional array object // whose elements contain// references to objects of type // String.//Create the array object Object v1 = Array.newInstance(Class.forName( "java.lang.String"), 3);//Populate the array object for(int i = 0; i<Array.getLength(v1); i++){ Array.set(v1, i, "a"+i);}//end for loop //Display the datafor(int i = 0; i<Array.getLength(v1); i++){ System.out.print(Array.get(v1, i) + " "); }//end for loopSystem.out.println(); System.out.println();//Create, populate, and display a // rectangular two-dimensional// array object tree whose // elements contain references// to objects of type String. //First create an array object of// type int required as a // parameter to the newInstance// method. Populate it to later // specify a rectangular array// object tree with two rows and // three columns.Object v2 = Array.newInstance( int.class, 2);Array.setInt(v2, 0, 2); Array.setInt(v2, 1, 3);//Now create the actual two- // dimensional array object tree.Object v3 = Array.newInstance( Class.forName("java.lang.String"), (int[])v2);//Populate the leaf elements with // references to objects of type// String. 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//Display the data encapsulated // in the String objects.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();//Now illustrate sorting and // searching using methods of// the arrays class. //Create the array objectObject 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 loopSystem.out.println(); //Sort array data into// ascending order. Arrays.sort((Object[])v4); //Display the sorted datafor(int i = 0; i<Array.getLength(v4); i++){ System.out.print(Array.get(v4, i) + " "); }//end for loopSystem.out.println(); //Search for an existing StringSystem.out.println( Arrays.binarySearch((Object[])v4, "c5"));//Search for a non-existing String System.out.println(Arrays.binarySearch((Object[])v4,"c4")); }catch(ClassNotFoundException e){System.out.println(e);} }//end main}//end class Array08

-end-

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