标签:
for(int i=0;i<10;i++){do{k=ran.nextInt(100);}while(intList.contains(k));intList.add(k);
通过collections.sort()方法对Intager类型的list排序方法
public void testSort1(){/*** 通过Collection.sort方法对Integer泛型的List排序*/List<Integer> intList=new ArrayList<Integer>();Random ran=new Random();Integer k;for(int i=0;i<10;i++){do{k=ran.nextInt(100);}while(intList.contains(k));intList.add(k);System.out.println("成功添加元素"+k);}System.out.println("------排序前-----");for(int temp1:intList){System.out.println(temp1);}Collections.sort(intList);System.out.println("------排序后-----");for(int temp2:intList){System.out.println(temp2);}}
public void testSort2() {List<String> stringList = new ArrayList<String>();stringList.add("lalala");stringList.add("hello");stringList.add("nonono");System.out.println("-----排序前----");for (String st : stringList) {System.out.println(st);}Collections.sort(stringList);System.out.println("-----排序后----");for (String st : stringList) {System.out.println(st);}}
public class Student implements Comparable<Student>public int compareTo(Student o) {return this.id.compareTo(o.id);}public void testSort3(){List<Student> studentList=new ArrayList<Student>();studentList.add(new Student(3+" ","小明", null));studentList.add(new Student(2+" ","小红", null));studentList.add(new Student(1+" ","小方", null));System.out.println("----排序前----");for(Student temp:studentList){System.out.println("学生"+temp.name);}Collections.sort(studentList);System.out.println("----排序后----");for(Student temp:studentList){System.out.println("学生"+temp.name);}}
标签:
原文地址:http://www.cnblogs.com/songwenyi/p/5637872.html