java.util.Arrays.copyOf 和 copyOfRange方法

java.util.Arrays.copyOf 和 copyOfRange方法

Modifier and Type 方法 描述
static boolean[] copyOf(boolean[] original, int newLength) 复制指定的数组,截断或填充 false (如有必要),因此副本具有指定的长度。
static byte[] copyOf(byte[] original, int newLength) 复制指定的数组,使用零截断或填充(如有必要),以使副本具有指定的长度。
static char[] copyOf(char[] original, int newLength) 复制指定的数组,截断或填充空字符(如有必要),以便复制具有指定的长度。
static double[] copyOf(double[] original, int newLength) 复制指定的数组,使用零截断或填充(如有必要),以使副本具有指定的长度。
static float[] copyOf(float[] original, int newLength) 复制指定的数组,使用零截断或填充(如有必要),以使副本具有指定的长度。
static int[] copyOf(int[] original, int newLength) 复制指定的数组,使用零截断或填充(如有必要),以使副本具有指定的长度。
static long[] copyOf(long[] original, int newLength) 复制指定的数组,使用零截断或填充(如有必要),以使副本具有指定的长度。
static short[] copyOf(short[] original, int newLength) 复制指定的数组,使用零截断或填充(如有必要),以使副本具有指定的长度。
static T[] copyOf(T[] original, int newLength) 复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。
static T[] copyOf(U[] original, int newLength, Class newType) 复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。
static boolean[] copyOfRange(boolean[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static byte[] copyOfRange(byte[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static char[] copyOfRange(char[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static double[] copyOfRange(double[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static float[] copyOfRange(float[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static int[] copyOfRange(int[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static long[] copyOfRange(long[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static short[] copyOfRange(short[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static T[] copyOfRange(T[] original, int from, int to) 将指定数组的指定范围复制到新数组中。
static T[] copyOfRange(U[] original, int from, int to, Class newType) 将指定数组的指定范围复制到新数组中。

copyOf(int[] original, int newLength)

import java.util.Arrays;

public class TestCopyOf {
    public static void main(String[] args) {
        test();

    }
    //测试  copyOf()方法
    public static void test(){
        int[] a = {1,2,3,5,10,8};
        int[] copy = Arrays.copyOf(a, 4);
        System.out.println("复制数组的前4位:"+Arrays.toString(copy));
    }
}

复制数组的前4位:[1, 2, 3, 5]

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

import java.util.Arrays;

public class TestCopyOfRange {
    public static void main(String[] args) {
        test();

    }
    //测试  copyOfRange()方法
    public static void test(){
        int[] a = {1,2,3,5,10,8};
        int[] copy = Arrays.copyOfRange(a, 2,5);
        System.out.println("复制数组的2到4位:"+Arrays.toString(copy));
    }
}

复制数组的2到4位:[3, 5, 10]
原文地址:https://www.cnblogs.com/sweetorangezzz/p/12918802.html