服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java教程之二个arraylist排序的示例分享

java教程之二个arraylist排序的示例分享

2019-11-13 12:51java教程网 JAVA教程

常常遇到数组排序的问题,下面提供二个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);即可 

延伸 · 阅读

精彩推荐