Java API Classes Used In This Chapter


Manipulating Arrays With The java.util.Arrays Class

The Java platform makes it easy to perform common array manipulations such as searching and sorting with the java.util.Arrays class. Example 8.14 offers a short program that shows the Arrays class in action sorting an array of integers. Figure 8-26 shows the results of running this program.

Example 8.14: ArraySortApp.java

image from book
 1     import java.util.*; 2 3     public class ArraySortApp { 4       public static void main(String[] args){ 5        int[] int_array = {100, 45, 9, 1, 34, 22, 6, 4, 3, 2, 99, 66}; 6 7        for(int i=0; i<int_array.length; i++){ 8         System.out.print(int_array[i] + " "); 9        } 10       System.out.println(); 11 12       Arrays.sort(int_array); 13 14       for(int i=0; i<int_array.length; i++){ 15         System.out.print(int_array[i] + " "); 16       } 17       System.out.println(); 18      } // end main() method 19     } // end ArraySortApp class definition
image from book

image from book
Figure 8-26: Results of Running Example 8.14




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net