ArrayList类的测试

//ArrayList的测试

 

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class ArrayListTest
{
 public static void printElements(Collection c)
 {
  Iterator i = c.iterator();
  while (i.hasNext())
  {
   System.out.println(i.next());
  }
 }
 
    public static void main(String[] args)
 {
  Student s1 = new Student(12 , "zhangsan");
  Student s2 = new Student(11 , "wangwu");
  Student s3 = new Student(13 , "lisi");
  Student s4 = new Student(12 , "mybole");
  List<Student> l = new ArrayList<Student>();
  l.add(s1);
  l.add(s2);
  l.add(s3);
  l.add(s4);
  Collections.sort(l);
   Collections.sort(l,new Student.StudentComparator());
  // Collections.sort(l,Collections.reverseOrder());
//   Collections.frequency(c , o)
//   (o == null ? e == null : o.equals(e))
  printElements(l);
 }
}

class Student implements Comparable
{
 int    num;
 
 String name;
 
 
 static class StudentComparator implements Comparator
 {
  public int compare(Object obj1 , Object obj2)
  {
   Student st1 = (Student) obj1;
   Student st2 = (Student) obj2;
   int result = st1.num>st2.num ? 1 : (st1.num==st2.num ? 0 : -1);
   if (0==result)
   {
    result = st1.name.compareTo(st2.name);
   }
   return result;
  }
 }
 
 
 Student(int num , String name)
 {
  this.name = name;
  this.num = num;
 }
 
 public int compareTo(Object obj)
 {
  Student s = (Student) obj;
  return num>s.num ? 1 : (num==s.num ? 0 : -1);
 }
 
 public String toString()
 {
  return "num="+num+","+"name="+name;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值