go to previous page   go to home page   go to next page

Answer:

No, the class must explicitly say that it implements Comparable


Sorting Strings the Easy Way

Strings certainly implement Comparable. Here is our previous program that sorts an array of Strings, but now using the sort() method from Arrays:

import java.util.*;

public class ArraySortTesterStrings
{
   public static void main ( String[] args )
  {
    String[] strArray = { "bat", "ant", "dog", "cat", "eel", "ibx", "fox", "gnu", "cow", "pig" };
    
    // print out the array
    System.out.println("Before: "); 
    for ( String str : strArray )
      System.out.print( str + ", " ); 
    System.out.println( );
    
    // sort the array
    Arrays.sort( strArray );
  
    // print out the array
    System.out.println("After: "); 
    for ( String str : strArray )
      System.out.print( str + ", " ); 
    System.out.println( );
 
   }
}

QUESTION 14:

Could Array.sort( Object ) be used to sort an array of Box references?


go to previous page   go to home page   go to next page