控制台程序。
1 public class Person implements Comparable<Person> { 2 // Constructor 3 public Person(String firstName, String surname) { 4 this.firstName = firstName; 5 this.surname = surname; 6 } 7 8 @Override 9 public String toString() { 10 return firstName + " " + surname; 11 } 12 13 // Compare Person objects 14 public int compareTo(Person person) { 15 int result = surname.compareTo(person.surname); 16 return result == 0 ? firstName.compareTo(person.firstName) : result; 17 } 18 19 private String firstName; // First name of person 20 private String surname; // Second name of person 21 }
使用sort()方法对列表排序时,必须通过某种方式确定列表中对象的顺序。最合适的方式就是在Person类中实现Comparable<>接口。Comparable<>只声明了comparableTo()方法。
如果集合中存储的对象的类型实现了Comparable<>接口,就可以把集合对象作为参数传送给sort()方法。
1 import java.util.Vector; 2 import java.util.ListIterator; 3 import java.util.Collections; 4 import java.io.*; 5 6 public class TryVector { 7 public static void main(String[] args) { 8 Person aPerson = null; // A person object 9 Vector<Person> filmCast = new Vector<>(); 10 11 // Populate the film cast 12 while(true) { // Indefinite loop 13 aPerson = readPerson(); // Read in a film star 14 if(aPerson == null) { // If null obtained... 15 break; // We are done... 16 } 17 filmCast.add(aPerson); // Otherwise, add to the cast 18 } 19 20 int count = filmCast.size(); 21 System.out.println("You added " + count + (count == 1 ? " person": " people") + " to the cast:"); 22 // Show who is in the cast using an iterator 23 ListIterator<Person> thisLot = filmCast.listIterator(); 24 25 while(thisLot.hasNext()) { // Output all elements 26 System.out.println( thisLot.next()); 27 } 28 System.out.println(" The vector currently has room for " + (filmCast.capacity() - count) + " more people."); 29 30 // Now sort the vector contents and list it 31 Collections.sort(filmCast); 32 System.out.println(" The cast in ascending sequence is:"); 33 for(Person person : filmCast) { 34 System.out.println(person); 35 } 36 } 37 38 // Read a person from the keyboard 39 static Person readPerson() { 40 // Read in the first name and remove blanks front and back 41 String firstName = null; 42 String surname = null; 43 System.out.println(" Enter first name or ! to end:"); 44 try { 45 firstName = keyboard.readLine().trim(); // Read and trim a string 46 47 if(firstName.charAt(0) == '!') { // Check for ! entered 48 return null; // If so, we are done... 49 } 50 51 // Read in the surname, also trimming blanks 52 System.out.println("Enter surname:"); 53 surname = keyboard.readLine().trim(); // Read and trim a string 54 } catch(IOException e) { 55 System.err.println("Error reading a name."); 56 e.printStackTrace(); 57 System.exit(1); 58 } 59 return new Person(firstName,surname); 60 } 61 62 63 static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); 64 }
把filmCast对象传送给Collections类的sort()静态方法,就会导致对集合中的对象排序。
键盘对象是InputStreamReader对象中封装的System.in,而InputStreamReader对象封装在BufferedReader对象中。InputStreamReader对象可以把输入从字节流System.in转换为字符。BufferedReader对象缓存了从InputStreamReader读入的数据。因为输入包含一系列字符串,而每个字符串占一行,所以readLine()方法可以完成我们需要的工作。