package myPro1;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
//Collections,工具类的常用算法
public class T7 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(8);
list.add(200);
list.add(3);
list.add(5);
list.add(7);
System.out.println(list);
System.out.println("============================");
Collections.sort(list);// 排序
System.out.println(list);
// 二分查找,用之前,要先排序sort
int index = Collections.binarySearch(list, 7);
//7,排在list的下标2,所以打印下标,2
System.out.println(index);
System.out.println("============================");
//Collections.shuffle(list);// 随机洗牌,每次洗的牌都不一样
System.out.println(list);
System.out.println("============================");
int min = Collections.min(list);//最小值
int max = Collections.max(list);//最大值
System.out.println("min=" + min);
System.out.println("max=" + max);
System.out.println("============================");
//Collections.swap(list, 0, 3);//交换位置,下标0和下标3
Collections.reverse(list);//反转
System.out.println(list);
}
}
Collections,工具类的常用算法
最新推荐文章于 2023-04-04 10:44:10 发布