Java的Arrays和Collections常用方法

一、Arrays

1.常用方法

  1. public static boolean equals(int[] a, int[] a2)  判断两个数组是否相等
  2. public static String toString(int[] a)                 输出数组信息
  3. public static void fill(int[] a, int val)                 将指定值填充到数组之中
  4. public static void sort(int[] a)                          将数组进行排序
  5. 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.常用方法

排序操作

  1. public static void reverse(List<?> list)                                            反转list中元素的顺序
  2. public static void shuffle(List<?> list)                                              对list集合元素进行随机排序
  3. public static <T extends Comparable<? super T>> void sort(List<T> list)   根据元素的自然排序对指定list集合元素按升序排序
  4. public static <T> void sort(List<T> list, Comparator<? super T> c)  根据指定的comparator产生的顺序对list集合元素进行排序
  5. public static void swap(List<?> list, int i, int j)                                    将指定list集合中的i处元素和j处元素进行交互

查找,替换

  1. public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)  根据元素的自然排序,返回给定集合中的最大元素
  2. public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)   根据comparator指定的顺序,返回给定集合中的最大元素
  3. public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
  4. public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
  5. public static int frequency(Collection<?> c, Object o)                                                         返回指定集合中指定元素的出现次数
  6. public static <T> void copy(List<? super T> dest, List<? extends T> src)
  7. 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);
}

3.结果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值