java常用库函数

每日一言

The strong don’t win. The winners are the strong ones. – Tsutomu Iwamura
from Kuroko’s Basketball - duplicate

数组

java.util.Arrays

1. 数组复制

  • Arrays.copyOf():创建一个新数组,包含源数组的指定元素

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /**
    * 复制指定的数组,截取或用默认值填充(如必要),以使副本具有指定的长度
    * @param original 要复制的数组
    * @param newLength 副本的长度
    * @return 原始数组的副本,截取或填充以获得指定的长度
    */
    public static <T> T[] copyOf(T[] original, int newLength); //函数原型

    int[] original = {1, 2, 3};
    int[] copy = Arrays.copyOf(original, 5); // 结果: [1, 2, 3, 0, 0]
  • Arrays.copyOfRange():复制数组的指定范围到新数组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * 将指定数组的指定范围复制到新数组
    * @param original 要复制的数组
    * @param from 起始索引(包括)
    * @param to 结束索引(不包括)
    * @return 包含指定范围内元素的新数组
    */
    public static <T> T[] copyOfRange(T[] original, int from, int to);

    int[] original = {1, 2, 3, 4, 5};
    int[] copy = Arrays.copyOfRange(original, 1, 4); // 结果: [2, 3, 4]

2. 排序相关

  • Arrays.sort():使用优化的双轴快速排序(基本类型)或归并排序(对象类型)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * 对数组的指定范围进行排序
    * @param a 要排序的数组
    * @param fromIndex 起始索引(包括)
    * @param toIndex 结束索引(不包括)
    */
    public static void sort(int[] a);
    public static void sort(Object[] a, int fromIndex, int toIndex);

    int[] array = {5, 2, 8, 1, 3};
    Arrays.sort(array); // 结果: [1, 2, 3, 5, 8]
  • Arrays.parallelSort()(Java8+):利用多线程进行排序,适合大数组

    1
    2
    3
    int[] largeArray = new int[100000];
    // 填充数组...
    Arrays.parallelSort(largeArray);

3. 搜索相关

  • Arrays.binarySearch():在已排序数组中进行二分查找

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * 使用二分查找算法在指定的已排序数组中搜索指定的值
    * @param a 要搜索的排序数组
    * @param key 要搜索的值
    * @return 找到的索引;如果未找到,则为负值
    */
    public static int binarySearch(Object[] a, Object key);
    public static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key);

    int[] sortedArray = {1, 2, 3, 5, 8};
    int index = Arrays.binarySearch(sortedArray, 5); // 结果: 3

4. 填充相关

  • Arrays.fill():使用指定值填充整个数组或数组的一部分

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    /**
    * 将指定的值分配给数组的指定范围的每个元素
    * @param a 要填充的数组
    * @param fromIndex 起始索引(包括)
    * @param toIndex 结束索引(不包括)
    * @param val 要存储在数组元素中的值
    */
    public static void fill(Object[] a, Object val);
    public static void fill(Object[] a, int fromIndex, int toIndex, Object val);

    int[] array = new int[5];
    Arrays.fill(array, 10); // 结果: [10, 10, 10, 10, 10]
  • Arrays.setAll():使用生成器函数填充数组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /**
    * 使用提供的生成函数设置数组的所有元素
    * @param array 要填充的数组
    * @param generator 计算每个元素值的函数
    */
    public static <T> void setAll(T[] array, IntFunction<? extends T> generator);

    int[] array = new int[5];
    Arrays.setAll(array, i -> i * 2); // 结果: [0, 2, 4, 6, 8] i表示索引,返回值为索引的二倍
  • Arrays.parallelSetAll():java8+ 并行填充数组

    1
    2
    int[] largeArray = new int[10000];
    Arrays.parallelSetAll(largeArray, i -> i * 2);
  • Arrays.parallePrefix() Java8 +:对数组进行前缀操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /**
    * 对数组执行前缀操作
    * @param array 要修改的数组
    * @param op 要执行的操作
    */
    public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op);

    int[] array = {1, 2, 3, 4};
    Arrays.parallelPrefix(array, (x, y) -> x + y); // 结果: [1, 3, 6, 10]

System.arrycopy

  • System.arrcopy() 是Java 中一个原生的数组复制方法,它提供了高效的数组部分或全部元素复制功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 将源数组的一部分复制到目标数组
* @param src 源数组
* @param srcPos 源数组中的起始位置
* @param dest 目标数组
* @param destPos 目标数组中的起始位置
* @param length 要复制的数组元素的数量
*/
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

// 示例1:基本数组复制
int[] source = {1, 2, 3, 4, 5};
int[] dest = new int[5];
System.arraycopy(source, 0, dest, 0, source.length);
// dest 现在包含: {1, 2, 3, 4, 5}

// 示例2:部分复制
int[] source = {1, 2, 3, 4, 5};
int[] dest = {10, 20, 30, 40, 50};
System.arraycopy(source, 2, dest, 1, 2);
// dest 现在包含: {10, 3, 4, 40, 50}

// 示例3:在数组中插入元素
int[] array = {1, 2, 3, 5};
int[] newArray = new int[5];
System.arraycopy(array, 0, newArray, 0, 3);
newArray[3] = 4;
System.arraycopy(array, 3, newArray, 4, 1);
// newArray 现在包含: {1, 2, 3, 4, 5}

以后遇到再更新…


java常用库函数
http://blog.ulna520.com/2025/03/19/java常用库函数_20250319_221400/
Veröffentlicht am
March 19, 2025
Urheberrechtshinweis