欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 20:48:52
示例1复制代码 代码如下:package com.yonyou.test;import java.util.arraylist;import java.util.colle...

示例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);即可