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]