Skip to content

Arrays

About 991 wordsAbout 3 min

2025-05-06

Arrays类是在java.util包里面。

一、Arrays源码学习

Arrays中主要涉及的方法有:

  • sort
  • binarySearch
  • fill
  • copyOf
  • copyOfRange
  • asList

Arrays.sort(int[] a)

都是静态方法,直接使用类名.方法名使用

下面这些都是对基础了类型,默认生序的。

public static void sort(int[] a);
public static void sort(int[] a, int fromIndex, int toIndex);
// ...
// 可以对int[], long[], char[], short[], byte[], float[], double[]等基础类型数组进行排序
//

public static <T> void sort(T[] a, Comparator<? Super T> c)

这个方法对基础类型的包装类型的排序,可以传入一个比较器,来实现正序或者逆序排序。

还有一个类似的方法:

public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? Super T> c)

public static <T> void sort(T[] a, Comparator<? super T> c) {
    if (c == null) {
        sort(a);
    } else {
        if (LegacyMergeSort.userRequested)
            legacyMergeSort(a, c);
        else
            TimSort.sort(a, 0, a.length, c, null, 0, 0);
    }
}

比如下面例子;

Integer[] arr2 = new Integer[] {1, 2, 34};
Arrays.sort(arr2, (a, b) -> b - a);
Arrays.sort(arr2, (a, b) -> {
    return b - a;
});
Arrays.sort(arr2, Collections.reverseOrder());

public static int binarySearch(long[] a, long key)

最常用的二分查找,需要保持原数组有序。

public static void fill(int[] a, int val)

类似方法:

public static void fill(int[], int fromIndex, int toIndex, int val)

public static void fill(int[] a, int val) {
    for (int i = 0, len = a.length; i < len; i++)
      	// 全部填充为val
        a[i] = val;
}

public static int[] copyOf(int[] original, int newLength)

复制数组,original表示原来数组,newLength表示新数组的长度。

public static int[] copyOf(int[] original, int newLength) {
  	// 创建一个长为newLength的新数组
    int[] copy = new int[newLength];
  	// 通过底层System.arraycopy方法将元素拷贝到新数组里面。
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
}

补充内容:

    int[] arr = new int[]{3, 2, 4, 1, 5};
    int[] arr2 = new int[10];
    // srcPos + length 
    System.arraycopy(arr, 0, arr2, 1, 3);

    // [from, to)
    int[] ints = Arrays.copyOfRange(arr, 1, 2);

    for (int x : arr2) {
        System.out.println(x);
    }

public static int[] copyOfRange(int[] original, int from, int to)

顾名思义。

public static int[] copyOfRange(int[] original, int from, int to) {
  	// to - from表示数组长度
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    int[] copy = new int[newLength];
    System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
    return copy;
}

public static <T> List<T> asList(T... a)

T... a底层转化为T[] x的数组。

public static <T> List<T> asList(T... a) {
  	// 这里的ArrayList不是java.util里面的类,是Arrays类中的内部类
    return new ArrayList<>(a);
}

public static String toString(int[] a)

通过StringBuilder 循环拼接数组元素。

public static InStream stream(int[] array)

public static IntStream stream(int[] array) {
    return stream(array, 0, array.length);
}

二、Arrays内部类ArrayList

Changelog

6/3/25, 1:49 AM
View All Changelog
  • d3a6d-Merge branch 'dev1'on

求求了,快滚去学习!!!

求求了求求了,快去学习吧!

【题单】贪心算法

不知道方向的时候,可以多看看书,书会给你指明下一步该干什么,加油!