Collections类
1.概述:针对Collection集合操作的工具类
2.常用方法:
(1)public static <T> void sort(List<T> list)排序,默认按照自然顺序
(2)public static <T> int binarySearch(List<?> list,T key)二分查找
(3)public static <T> T max(Collection<?> coll)获取最大值
(4)public static void reverse(List<?> list)反转
(5)public static void shuffle(List<?> list)随机置换
例:
- import java.util.ArrayList;
- import java.util.Collections;
-
- public class CollectionsDemo {
- public static void main(String[] args) {
- ArrayList<Integer> al = new ArrayList<Integer>();
- al.add(7);
- al.add(31);
- al.add(9);
- al.add(45);
- al.add(96);
- al.add(33);
-
-
- Collections.sort(al);
- System.out.println("sort:" + al);
-
- System.out.println("---------------------------");
-
-
- int index = Collections.binarySearch(al, 33);
- System.out.println("binarySearch:" + index);
-
- System.out.println("---------------------------");
-
-
- int max = Collections.max(al);
- System.out.println("max:" + max);
-
- System.out.println("---------------------------");
-
-
- Collections.reverse(al);
- System.out.println("reverse:" + al);
-
- System.out.println("---------------------------");
-
- Collections.shuffle(al);
- System.out.println("shuffle:" + al);
-
- }
- }