1 |
Handout B for e02 |
CS56 F17 |
Handout B, p. 1: Useful Reference Items
Here are a few reminders of things we discussed in class, but that you might reasonably need a “reference” for if you were using them in the real world.
The interface java.util.Comparator<T>
includes the following
method signature:
int |
compare(T o1, T o2) |
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. |
The interface java.lang.Comparable<T>
includes the following
method signature:
int |
compareTo(T o) |
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. |
The class java.util.ArrayList<E>
includes this method:
void |
sort(Comparator<? super E> c) |
Sorts this list according to the order induced by the specified Comparator . |
The class java.util.Collections
contains the following static method:
static <T extends Comparable<? super T>> void |
sort(List<T> list) |
Sorts the specified list into ascending order, according to the natural ordering of its elements. |
And finally, java.lang.String
implements Comparable<String>
in the
way that you would expect.
Handout B, p. 2: Sample Output
$ java StudentMain
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
Student(2352353,"Alice",3.97,CMPEN)
Student(2888888,"Danielle",4.00,CMPSC)
Student(1152353,"Ed",2.90,CMPEN)
$ java StudentMain name
Student(2352353,"Alice",3.97,CMPEN)
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
Student(2888888,"Danielle",4.00,CMPSC)
Student(1152353,"Ed",2.90,CMPEN)
$ java StudentMain perm
Student(1152353,"Ed",2.90,CMPEN)
Student(2352353,"Alice",3.97,CMPEN)
Student(2888888,"Danielle",4.00,CMPSC)
Student(6234567,"Bob",3.25,CMPSC)
Student(7152353,"Charlie",3.96,CMPSC)
$
End of Handout