java基礎總結4

來源:互聯網
上載者:User

java基礎總結4
Collections工具類
是java集合架構中,用來操作集合對象的工具類
也是java集合架構的成員
sort()排序方法
Collections。sort()
List
List<封裝類>
List
Comparable & Comparator簡介
Comparable介面,是可比較的
實現該介面表示:這個類的執行個體可以比較大小,可以進行自然排序
定義了預設的比較規則
其實作類別需實現compareTo()方法
compareTo()方法返回正數表示大,負數表示小,0表示相等
Comparator介面比較工具介面
用於定義臨時比較規則,而不是預設比較規則
其實作類別需要實現compare()方法
Comparator和Comparable都是java集合架構的成員

案例:

 

 public class Student implements Comparable{       String id;       String name;public Student(String ID, String st_Name) {// TODO Auto-generated constructor stubthis.id = ID;this.name = st_Name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic int compareTo(Student o) {// TODO Auto-generated method stubreturn this.id.compareTo(o.id);}       }

public class StudentComparator implements Comparator {@Overridepublic int compare(Student arg0, Student arg1) {// TODO Auto-generated method stubreturn arg0.name.compareTo(arg1.name);}}

public class CollectionTest {/** * 通過Collections.sort()方法,對Integer泛型的List進行排序 *  */public void testSort1(){List integerList = new ArrayList();Random random = new Random();Integer k;for(int i = 0; i < 10;i++){do{k = random.nextInt(100);}while(integerList.contains(k));integerList.add(k);System.out.println("成功添加整數:" +  k);}System.out.println("排序前--------------------");for (Integer integer : integerList) {System.out.println("元素:" + integer);}Collections.sort(integerList);System.out.println("排序後---------------------");for (Integer integer : integerList) {System.out.println("元素:" + integer);}}/* *  對String泛型的List進行排序 *   * */public void testSort2(){List stringList = new ArrayList();stringList.add("del");stringList.add("lenovo");stringList.add("ios");stringList.add("apple");System.out.println("排序前----------------");for(String string:stringList){System.out.println("元素:" + string);}Collections.sort(stringList);System.out.println("排序後------------------");for(String string:stringList){System.out.println("元素:" + string);}}/* * 對其他類型的泛型List進行排序 * */public void testSort3(){List studentList = new ArrayList();Random random = new Random();studentList.add(new Student(random.nextInt(1000) + "","xiaoming"));studentList.add(new Student(random.nextInt(1000) + "","xiaohong"));studentList.add(new Student(random.nextInt(1000) + "","xiaobing"));studentList.add(new Student(random.nextInt(1000) + "","abcde"));System.out.println("排序前-------------");for (Student student : studentList) {System.out.println("學生:" + student.id + "   "+ student.name);}Collections.sort(studentList);System.out.println("排序後------------");for (Student student : studentList) {System.out.println("學生:" + student.id + "  "+ student.name);}Collections.sort(studentList,new StudentComparator());System.out.println("按照姓名排序後-------------");for (Student student : studentList) {System.out.println("學生:" + student.id + "  "+ student.name);}}public static void main(String[] args) {// TODO Auto-generated method stub        CollectionTest ct = new CollectionTest();//        ct.testSort1();//        ct.testSort2();        ct.testSort3();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.