Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //****************************************************
- // This program demonstrates the Arrays.sort method. *
- //****************************************************
- import java.util.Arrays; //for Arrays.sort()
- import java.io.File;
- import java.util.ArrayList;
- //This example demonstrates that the static Arrays.sort() method
- // works with objects that implement the Comparable interface
- public class SortDemo
- {
- public static void main(String [] args)
- {
- String[] lakers = new String[4];
- lakers[0] = "kobe";
- lakers[1] = "zo";
- lakers[2] = "ginobli";
- lakers[3] = "gasol";
- // Arrays.sort()can work on objects that implement
- // the Comparable interface
- for(String s: lakers )
- System.out.print( s + " " );
- System.out.println();
- Arrays.sort( lakers );
- for(String s: lakers )
- System.out.print( s + " " );
- System.out.println();
- Integer[] numbers = { 9, 2, 7, 12, 1 }; //auto box
- // Display the array elements unsorted.
- for(Integer i: numbers )
- System.out.print( i + " " );
- System.out.println();
- // Sort the array.
- Arrays.sort(numbers);
- // Display the array elements sorted.
- for(Integer i: numbers )
- System.out.print( i + " " );
- System.out.println();
- /*
- //run-time error since objects aren't mutually comparable
- Object[] foo = new Object[2];
- foo[0] = new String("joe");
- foo[1] = new Integer( 365 );
- //Arrays.sort(foo); //run-time error since objects aren't mutually comparable
- for(int i =0; i< foo.length; i++)
- System.out.println(foo[i]);
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement