Java基础之集合框架——使用集合Vector<>挑选演员(TryVector)
2013-11-18 21:34
399 查看
控制台程序。
使用sort()方法对列表排序时,必须通过某种方式确定列表中对象的顺序。最合适的方式就是在Person类中实现Comparable<>接口。Comparable<>只声明了comparableTo()方法。
如果集合中存储的对象的类型实现了Comparable<>接口,就可以把集合对象作为参数传送给sort()方法。
把filmCast对象传送给Collections类的sort()静态方法,就会导致对集合中的对象排序。
键盘对象是InputStreamReader对象中封装的System.in,而InputStreamReader对象封装在BufferedReader对象中。InputStreamReader对象可以把输入从字节流System.in转换为字符。BufferedReader对象缓存了从InputStreamReader读入的数据。因为输入包含一系列字符串,而每个字符串占一行,所以readLine()方法可以完成我们需要的工作。
public class Person implements Comparable<Person> { // Constructor public Person(String firstName, String surname) { this.firstName = firstName; this.surname = surname; } @Override public String toString() { return firstName + " " + surname; } // Compare Person objects public int compareTo(Person person) { int result = surname.compareTo(person.surname); return result == 0 ? firstName.compareTo(person.firstName) : result; } private String firstName; // First name of person private String surname; // Second name of person }
使用sort()方法对列表排序时,必须通过某种方式确定列表中对象的顺序。最合适的方式就是在Person类中实现Comparable<>接口。Comparable<>只声明了comparableTo()方法。
如果集合中存储的对象的类型实现了Comparable<>接口,就可以把集合对象作为参数传送给sort()方法。
import java.util.Vector; import java.util.ListIterator; import java.util.Collections; import java.io.*; public class TryVector { public static void main(String[] args) { Person aPerson = null; // A person object Vector<Person> filmCast = new Vector<>(); // Populate the film cast while(true) { // Indefinite loop aPerson = readPerson(); // Read in a film star if(aPerson == null) { // If null obtained... break; // We are done... } filmCast.add(aPerson); // Otherwise, add to the cast } int count = filmCast.size(); System.out.println("You added " + count + (count == 1 ? " person": " people") + " to the cast:"); // Show who is in the cast using an iterator ListIterator<Person> thisLot = filmCast.listIterator(); while(thisLot.hasNext()) { // Output all elements System.out.println( thisLot.next()); } System.out.println("\nThe vector currently has room for " + (filmCast.capacity() - count) + " more people."); // Now sort the vector contents and list it Collections.sort(filmCast); System.out.println("\nThe cast in ascending sequence is:"); for(Person person : filmCast) { System.out.println(person); } } // Read a person from the keyboard static Person readPerson() { // Read in the first name and remove blanks front and back String firstName = null; String surname = null; System.out.println("\nEnter first name or ! to end:"); try { firstName = keyboard.readLine().trim(); // Read and trim a string if(firstName.charAt(0) == '!') { // Check for ! entered return null; // If so, we are done... } // Read in the surname, also trimming blanks System.out.println("Enter surname:"); surname = keyboard.readLine().trim(); // Read and trim a string } catch(IOException e) { System.err.println("Error reading a name."); e.printStackTrace(); System.exit(1); } return new Person(firstName,surname); } static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); }
把filmCast对象传送给Collections类的sort()静态方法,就会导致对集合中的对象排序。
键盘对象是InputStreamReader对象中封装的System.in,而InputStreamReader对象封装在BufferedReader对象中。InputStreamReader对象可以把输入从字节流System.in转换为字符。BufferedReader对象缓存了从InputStreamReader读入的数据。因为输入包含一系列字符串,而每个字符串占一行,所以readLine()方法可以完成我们需要的工作。
相关文章推荐
- Java基础之集合框架——使用真的的链表LinkedList<>(TryPolyLine)
- Java基础之集合框架——使用堆栈Stack<>对象模拟发牌(TryDeal)
- 使用Java实现面向对象编程--集合框架-->Vector和ArrayList
- Java基础---集合框架---迭代器、ListIterator、Vector中枚举、LinkedList、ArrayList、HashSet、TreeSet、二叉树、Comparator
- Java基础之集合框架——使用HashMap地图(TryPhoneBook1)
- Java基础知识强化之集合框架笔记73:如何选择使用哪种集合
- Java基础---集合框架---迭代器、ListIterator、Vector中枚举、LinkedList、ArrayList、HashSet、TreeSet、二叉树、Comparator
- JAVA基础学习之String、StringBuffer、StringBuilder、基本数据类型的使用、整形进制转换、集合Collection、Vector、ArrayList、LinkedList、HashSet、TreeSet等(3)
- 使用Java实现面向对象编程--集合框架-->List接口
- Java基础知识强化之集合框架笔记31:集合之泛型类的概述和基本使用
- 使用Java实现面向对象编程--集合框架-->迭代器Iterator
- java语言基础(75)——集合框架(Set集合的概述及使用)
- JAVA基础--JAVA API集合框架(ArrayList、HashSet、HashMap使用)14
- 黑马程序员——Java基础->集合框架
- 程序员_Java基础之<七>-集合框架
- java语言基础(71)——集合框架(增强for的概述和使用)
- Java基础知识强化之集合框架笔记57:Map集合之HashMap集合(HashMap<Student,String>)的案例
- Java基础知识强化之集合框架笔记55:Map集合之HashMap集合(HashMap<Integer,String>)的案例
- Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例
- Java基础知识强化之集合框架笔记09:Collection集合迭代器使用的问题探讨