java教程之二個arraylist排序的樣本分享_java

來源:互聯網
上載者:User

樣本1

複製代碼 代碼如下:

package com.yonyou.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
 public static void main(String[] args) {
  Student zlj = new Student("丁曉宇", 21);
  Student dxy = new Student("趙四", 22);
  Student cjc = new Student("張三", 11);
  Student lgc = new Student("劉武", 19);
  List<Student> studentList = new ArrayList<Student>();
  studentList.add(zlj);
  studentList.add(dxy);
  studentList.add(cjc);
  studentList.add(lgc);
  System.out.println("按照年齡排序:");
  Collections.sort(studentList, new SortByAge());
  for (Student student : studentList) {
   System.out.println(student.getName() + " / " + student.getAge());
  }
  System.out.println(" ========= ");
  System.out.println("按照姓名排序");
  Collections.sort(studentList, new SortByName());
  for (Student student : studentList) {
   System.out.println(student.getName() + " / " + student.getAge());
  }
 }
}
class SortByAge implements Comparator {
 public int compare(Object o1, Object o2) {
  Student s1 = (Student) o1;
  Student s2 = (Student) o2;
  if (s1.getAge() > s2.getAge())
   return 1;
  else if (s1.getAge() == s2.getAge()) {
   return 0;
  }
  return -1;
 }
}
class SortByName implements Comparator {
 public int compare(Object o1, Object o2) {
  Student s1 = (Student) o1;
  Student s2 = (Student) o2;
  if(s1.getName().compareTo(s2.getName()) < 0)
   return -1;
  else if (s1.getName().compareTo(s2.getName()) > 0) {
   return 1;
  }
  return 0;
 }
}
class Student{
 private int age;
 private String name;
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Student(String name,int age) {
  this.age = age;
  this.name = name;
 }
}

樣本2

常常遇到數組排序的問題.比如我有一個Person類,它的執行個體Object Storage Service在ArrayList數組中,現在要把ArrayList數組中 的Person對象按照年齡排序.
其實這種情況經常遇到.
下面給出原始碼:

1:Person.java文 件:

複製代碼 代碼如下:

public class Person{
     String name;
     int age;

 public Person(String name,int age){
     this.name = name;
     this.age = age;

 }
 public int getAge() {
     return age;
 }
 public void setAge(int age) {
     this.age = age;
 }
 public String getName() {
     return name;
 }
 public void setName(String name) {
     this.name = name;
 }
}


2:Mycomparator.java

複製代碼 代碼如下:

// 實現Comparator介面,也就是定義定序,你幾乎可以定義任何規則
package com.infoearth;
import java.util.*;
public class Mycomparator implements Comparator{

    public int compare(Object o1,Object o2) {
        Person p1=(Person)o1;
        Person p2=(Person)o2; 
       if(p1.age<p2.age)
           return 1;
       else
           return 0;
       }

}

3:ListSort.java

複製代碼 代碼如下:

package com.infoearth;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ListSort {
     public static void main(String[] args){
         ArrayList list = new ArrayList();
         list.add(new Person("lcl",28));
         list.add(new Person("fx",23));
         list.add(new Person("wqx",29));
         Comparator comp = new Mycomparator();
         Collections.sort(list,comp); 
         for(int i = 0;i<list.size();i++){
             Person p = (Person)list.get(i);
             System.out.println(p.getName());
         } 

     }

}

當然,如果你的List封裝的是基本類型或者String,則只要 Collections.sort(list);即可 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.