【CXY】JAVA应用 之 排序

来源:互联网 发布:九大排序算法 编辑:程序博客网 时间:2024/05/17 09:16

概述:

    1.本文阐述对List(Array乱入)的排序问题。

    2.Set和Map的排序 可使用TreeSet、TreeMap,见本博客的相关文章(下面提供连接)。

    3.想对一个List进行排序有2种方法(数组也适用):

       方法一:实现一个比较器Comparator。

       方法二:List中的对象实现Comparable接口的compareTo方法。

    4.完成上面一点后,集合使用Collections.sort,数组使用Arrays.sort进行排序。

package com.cxy.collection;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.List;import java.util.Set;/** * @author cxy */public class ListSortTest{public static void main(String[] args){List <Student> l=new ArrayList();Student s1 =new Student("小明",76);Student s2 =new Student("小黑",99);Student s3 =new Student("小白",60);l.add(s1);l.add(s2);l.add(s3);System.out.print("List排序前:");System.out.println(l);//第一种方式:普通的对象,然后实现一个比较器,使用下面的方法进行排序Collections.sort(l,new StudentComparator());System.out.print("List排序后:");System.out.println(l);System.out.println("=======================");//乱入的数组Student[] sa={s1,s2,s3};System.out.print("数组排序前:");System.out.println(sa[0].toString()+sa[1]+sa[2]);Arrays.sort(sa,new StudentComparator());System.out.print("数组排序后:");System.out.println(sa[0].toString()+sa[1]+sa[2]);System.out.println("============下面的都是ComparableStudent演示===========");//对象自身拥有排序能力List<ComparableStudent> l1=new ArrayList();ComparableStudent s11 =new ComparableStudent("较劲的小明",76);ComparableStudent s22 =new ComparableStudent("较劲的小黑",99);ComparableStudent s33 =new ComparableStudent("较劲的小白",60);//PS:较劲的意思可以理解为 争强好胜 o_ol1.add(s11);l1.add(s22);l1.add(s33);System.out.print("List排序前:");System.out.println(l1);Collections.sort(l1);System.out.print("List排序后:");System.out.println(l1);System.out.println("=======================");//数组再次乱入ComparableStudent[] cs={s11,s22,s33};System.out.print("数组排序前:");System.out.println(cs[0].toString()+cs[1]+cs[2]);Arrays.sort(cs);System.out.print("数组排序后:");System.out.println(cs[0].toString()+cs[1]+cs[2]);}}//一个普通的学生类,按照学生的学习成绩进行排序//这里只是为了快速展示排序而设计的“简陋”的类,所以不要追究 你这个类设计的不科学,成员变量应该私有等问题。class Student{public String name;  //姓名public int score;  //分数Student(String name,int score){this.name=name;this.score=score;}@Overridepublic String toString(){return "["+name+":"+score+"]";}}//一个学生的比较器,泛型定义了比较的范围是Student,也可以不定义泛型,再在compare里面进行强制转换class StudentComparator  implements Comparator<Student>{@Overridepublic int compare(Student s1, Student s2){if(s1.score>s2.score) return 1;else if(s1.score<s2.score) return -1;else return 0;}}/** * 一个实现了Comparable接口的compareTo方法的学生类 * compareTo的实现方式: *   x.compareTo(y)为例,当x小于y的时候,返回一个负数。当x大于y的时候,返回一个正数。相等的时候返回0. */class ComparableStudent implements Comparable<ComparableStudent>{public String name;  //姓名public int score;  //分数ComparableStudent(String name,int score){this.name=name;this.score=score;}@Overridepublic int compareTo(ComparableStudent s){if(score>s.score) return 1;else if(score<s.score) return -1;else return 0;}@Overridepublic String toString(){return "["+name+":"+score+"]";}}

PS:这里将很多类写在了一个文件里,目的就是为了方便大家拷贝参考。使用时不需要分开 直接拷贝运行。

 

 

相关文章连接:

《JAVA基础 之 Collection》

《JAVA基础 之 List》

《JAVA基础 之 Set》

 

声明:

1.原创文章,转载请标明并加本文连接。

2.更详尽的API请参见  http://docs.oracle.com/javase/7/docs/api/

3.文章反映个人愚见,如有异议欢迎讨论指正