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
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
Figure 8-26: Results of Running Example 8.14