No, the class must explicitly say that it implements Comparable
String
s certainly implement Comparable
.
Here is our previous program that sorts an array of String
s, 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( ); } }
Could Array.sort( Object )
be used to sort an array of Box
references?