一、Arrays
1.常用方法
- public static boolean equals(int[] a, int[] a2) 判断两个数组是否相等
- public static String toString(int[] a) 输出数组信息
- public static void fill(int[] a, int val) 将指定值填充到数组之中
- public static void sort(int[] a) 将数组进行排序
- public static int binarySearch(int[] a, int key) 对排序后的数组进行二分法检索指定的值
2.测试
@Test
public void test1() {
int[] arr1 = new int[]{1, 3, 2, 4};
int[] arr2 = new int[]{1, 2, 3, 4};
boolean isEquals = Arrays.equals(arr1, arr2);
System.out.println(isEquals);
}
@Test
public void test2() {
int[] arr1 = new int[]{1, 3, 2, 4};
System.out.println(Arrays.toString(arr1));
}
@Test
public void test3() {
int[] arr1 = new int[]{1, 3, 2, 4};
Arrays.fill(arr1, 10);;
System.out.println(Arrays.toString(arr1));
}
@Test
public void test4() {
int[] arr1 = new int[]{1, 3, 2, 4};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
}
@Test
public void test5() {
int[] arr1 = new int[]{1, 2, 3, 4};
int index = Arrays.binarySearch(arr1, 3); // 二分查找,前提数组有序
System.out.println(index);
}
3.结果
二、Collections
1.常用方法
排序操作
- public static void reverse(List<?> list) 反转list中元素的顺序
- public static void shuffle(List<?> list) 对list集合元素进行随机排序
- public static <T extends Comparable<? super T>> void sort(List<T> list) 根据元素的自然排序对指定list集合元素按升序排序
- public static <T> void sort(List<T> list, Comparator<? super T> c) 根据指定的comparator产生的顺序对list集合元素进行排序
- public static void swap(List<?> list, int i, int j) 将指定list集合中的i处元素和j处元素进行交互
查找,替换
- public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) 根据元素的自然排序,返回给定集合中的最大元素
- public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) 根据comparator指定的顺序,返回给定集合中的最大元素
- public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
- public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
- public static int frequency(Collection<?> c, Object o) 返回指定集合中指定元素的出现次数
- public static <T> void copy(List<? super T> dest, List<? extends T> src)
- public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) 使用新值替换list对象的所有旧值
2.测试
@Test
public void test11() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
Collections.reverse(list);
System.out.println(list);
}
@Test
public void test12() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
Collections.shuffle(list);
System.out.println(list);
}
@Test
public void test13() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
Collections.sort(list);
System.out.println(list);
}
@Test
public void test14() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
Collections.sort(list,(item1, item2) -> -(item1-item2));
System.out.println(list);
}
@Test
public void test15() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
Collections.swap(list, 1, 2);
System.out.println(list);
}
@Test
public void test21() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
Integer max = Collections.max(list);
System.out.println(max);
}
@Test
public void test25() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
int frequency = Collections.frequency(list, 2);
System.out.println(frequency);
}
@Test
public void test26() {
List<Integer> list = Arrays.asList(1,8,9,2,4,6,2);
List<Integer> dest = Arrays.asList(new Integer[list.size()]);
Collections.copy(dest, list);
System.out.println(dest);
}