任何类只要实现了Comparable接口就可以调用Java提供的Array.sort()函数或是是Collections.sort()函数对一个对象列表排序,在重写Comparable中的函数是我们可以自定义对列表的排序方式,比如要对一个学生列表先按年龄排序,然后如果年龄相同的话就按姓名的字典序排序,甚至可以添加更多的排序条件。一些就以这个例子用java代码简单实现一下。
我们要对学生排序,首先就要定义学生Student对象,并且要实现Comparable接口,从字面上就能很明显的看出,只有实现了该接口才有可比性嘛!哈哈
定义Student类:
[html] view plain copy <pre name="code" class="java">class Student implements Comparable<Student> {//因为在ArrayList中使用了泛型,所以要在此用<>表明类型,比如是对String类实现的接口就写String String name; int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } public int compareTo(Student tempStudent) {//从写接口中的函数 if (this.age != tempStudent.age) {//当年龄不相同时,则只按年龄的大小对学生进行排序 return this.age - tempStudent.age;//交换两者的顺序可以实现是升序还是降序 } else {//如果两个学生的年龄相同,那么我们就按其姓名排序 if (!this.getName().equals(tempStudent.getName())) {//如果前几个字母都一样,我们就不能进行比较,所以要对第一个不同的字母进行比较 int i = 0; while (i < this.name.length() && i < tempStudent.getName().length()) {//使用循序找到第一个不同的字母 if (this.name.charAt(i) != tempStudent.getName().charAt(i)) break; i++; } return this.name.charAt(i) - tempStudent.getName().charAt(i);//返回名字的比较值 } else//当名字相同是 return -1;//顺便返回一个值,因为这不会影响排序的结果,只是为了与前面的if搭配 } } public String toString() { return "name=" + name + ", age=" + age; } public String getName() { return this.name; }
上面定义了Student类,下面我们将Student类的对象添加的ArrayList的对象中,并是用sort函数排序:
包含main方法的类:
[java] view plain copy public class arraylist的使用 { public static void main(String[] args) { ArrayList<Student> stuList = new ArrayList<Student>(20);//设置stuList的接受类型是Student,设定默认容量是20个元素 stuList.add(new Student("ciong", 21)); stuList.add(new Student("xanarry", 11)); stuList.add(new Student("amith", 21)); stuList.add(new Student("jason", 19)); stuList.add(new Student("marray", 25)); stuList.add(new Student("blex", 21));//添加6个学生到stuList中 System.out.println("---original order---"); for (Iterator<Student> iterator = stuList.iterator(); iterator.hasNext(); ) {//使用迭代器遍历输入所有学生信息 System.out.println(iterator.next()); } //使用sort函数排序 //写法1: stuList.sort(null); ///写法2: Collections.sort(stuList); System.out.println("---sorted---"); for (Iterator<Student> iterator = stuList.iterator(); iterator.hasNext(); ) {//输入有序结果 System.out.println(iterator.next()); } } }
两次的输出结果如下:
[java] view plain copy ---original order--- name=ciong, age=21 name=xanarry, age=11 name=amith, age=21 name=jason, age=19 name=marray, age=25 name=blex, age=21
排序后:
[java] view plain copy ---sorted--- name=xanarry, age=11 name=jason, age=19 name=amith, age=21 name=blex, age=21 name=ciong, age=21 name=marray, age=25
