9. 3. 13. Array Reallocation |
|
You can make a clone of an array and make the new array have a different size. |
public static boolean[] copyOf (boolean[] original, int newLength)
public static byte[] copyOf (byte[] original, int newLength)
public static char[] copyOf (char[] original, int newLength)
public static double[] copyOf (double[] original, int newLength)
public static float [] copyOf (float[] original, int newLength)
public static int[] copyOf (int[] original, int newLength)
public static long[] copyOf (long[] original, int newLength)
public static short[] copyOf (short[] original, int newLength)
public static <T> T[] copyOf (T[] original, int newLength)
public static <T,U> T[] copyOf (U[] original, int newLength,java.lang.Class<? extends T[]> newType)
|
|
The last method allows you to upcast each element in the original array to a parent type. |
Another method similar to copyOf that is also added to Arrays in Java 6 is copyOfRange. |
copyOfRange copies a range of elements to a new array. |
Like copyOf, copyOfRange also provides overrides for each Java data type. |
Here are their signatures: |
public static boolean[] copyOfRange (boolean[] original,int from, int to)
public static byte[] copyOfRange (byte[] original,int from, int to)
public static char[] copyOfRange (char[] original,int from, int to)
public static double[] copyOfRange (double[] original,int from, int to)
public static float[] copyOfRange (float[] original,int from, int to)
public static int[] copyOfRange (int[] original, int from, int to)
public static long[] copyOfRange (long[] original, int from, int to)
public static short[] copyOfRange (short[] original, int from, int to)
public static <T> T[] copyOfRange (T[] original, int from, int to)
public static <T,U> T[] copyOfRange (U[] original, int from,int to, java.lang.Class<? extends T[]> newType)
|
|
Array reallocation example |
import java.util.Arrays;
public class ArrayReallocationDemo {
public static void main(String[] args) {
int[] data1 = new int[] { 1, 3, 5, 7, 9 };
printArray(data1);
int[] data2 = Arrays.copyOf(data1, 6);
data2[5] = 11;
printArray(data2);
int[] data3 = Arrays.copyOfRange(data1, 2, 10);
printArray(data3);
}
// print array elements
private static void printArray(int[] data) {
StringBuilder stringBuilder = new StringBuilder("[");
for (int i = 0; i < data.length; i++) {
stringBuilder.append(data[i]);
if (i < data.length - 1)
stringBuilder.append(", ");
}
stringBuilder.append("]");
System.out.println(stringBuilder);
}
}
|
|
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9, 11]
[5, 7, 9, 0, 0, 0, 0, 0] |