首页 > 代码库 > 升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)

升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)

 1 package Day28ketangzuoye; 2  3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.HashSet; 7 import java.util.List; 8 import java.util.Scanner; 9 10 public class DemoChars{11 12     public static void main(String[] args) {13         Scanner sc=new Scanner(System.in);14         System.out.println("请输入N个字符:");15         String str=sc.next();16         String[] arr=str.split(",");17 //        System.out.println(Arrays.toString(arr)); 18         19         List<String> list=new ArrayList<String>();20         for(int i=0;i<arr.length;i++){21             list.add(arr[i]);22         }23         System.out.println("原序输出:  "+list); 24         25         Collections.sort(list);26         System.out.println("升序输出: "+list);27         28         Collections.sort(list,new Comparator<String>(){29             @Override30             public int compare(String o1, String o2) {31                 return o2.compareTo(o1);32             }});33         System.out.println("降序输出:   "+list);34         35         Collections.reverse(list);36         System.out.println("反转指定列表中元素的顺序     "+list);37         38 //        Set set = new ArraySet(list);39 //        HashSet<String> set=new HashSet<String>(list);40 //        System.out.println("无序输出:  "+set); 41 //        升序  Collections.sort(list)    降序  Collections.reserve(list)   随机  Collections.shuffle(list)        42         43     }44 }

 

升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)