java教程之二個arraylist排序的示例分享。本站提示廣大學習愛好者:(java教程之二個arraylist排序的示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java教程之二個arraylist排序的示例分享正文
示例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類,它的實例對象存儲在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);便可