Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96361
  • 博文数量: 54
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-30 00:36
文章分类

全部博文(54)

文章存档

2010年(1)

2009年(52)

2008年(1)

我的朋友

分类: Java

2009-10-07 17:55:52

主要是提供了对列表的排序,而不是数组。
 
排序:Collections.sort()
(1)自然排寻(natural ordering );
(2)实现比较器(Comparator)接口。
取最大和最小的元素:Collections.max()、Collections.min()。
在已排序的List中搜索指定的元素:Collectons.binarySearch()。
class Student implements Comparable
{
 int num;
 String name;
 static class StudentComparator implements Comparator
 {
  public int compare(Object o1,Object o2)
  {
   Student s1=(Student)o1;
   Student s2=(Student)o2;
   int result=s1.num > s2.num ? 1 : (s1.num==s2.num ? 0 : -1);
   if(result==0)
   {
    result=s1.name.compareTo(s2.name);
   }
   return result;
  }
 }
 Student(int num,String name)
 {
  this.num=num;
  this.name=name;
 }
 
 public int compareTo(Object o)
 {
  Student s=(Student)o;
  return num > s.num ? 1 : (num==s.num ? 0 : -1);
 }
 public String toString()
 {
  return num+":"+name;
 }
}
阅读(484) | 评论(0) | 转发(0) |
0

上一篇:ArrayList

下一篇:

给主人留下些什么吧!~~