🧨🧨🧨🧨Collections集合工具类
public class demoCollection { public static void main(String[] args) { ArrayList<String> list=new ArrayList<>(); Collections.addAll(list,"a","b","c","d"); System.out.println(list);//[a, b, c, d] Collections.shuffle(list);//打乱顺序:打乱集合顺序 System.out.println(list); } }
public class demoColl { public static void main(String[] args) { ArrayList<Integer> list=new ArrayList<>(); list.add(1); list.add(5); list.add(3); Collections.sort(list); System.out.println(list);//[1,3,5] ArrayList<String> list1=new ArrayList<>(); list1.add("b"); list1.add("z"); list1.add("a"); Collections.sort(list1); System.out.println(list1);//[a, b, z] } }
public class dmeoSort { public static void main(String[] args) { ArrayList<Integer> list=new ArrayList<>(); list.add(5); list.add(1); list.add(2); System.out.println(list); Collections.sort(list,new Comparator<Integer>(){ //重写比较的规则 @Override public int compare(Integer o1,Integer o2){ return o2-o1;//降序[5, 2, 1] //return o1-o2;//升序//[1, 2, 5] } }); System.out.println(list); } }