Changing Array Size

Once an array is created, its size cannot be changed. If you want to change the size, you must create a new array and populates it using the values of the old array. For instance, the following code increases the size of numbers, an array of three ints, to 4.

int[] numbers = { 1, 2, 3 };
int[] temp = new int[4];
int length = numbers.length;
for (int j = 0; j < length; j++) {
    temp[j] = numbers[j];
}
numbers = temp;

A shorter way of doing this is by using the copyOf method of java.util.Arrays. For instance, this code creates a four-element array and copies the content of numbers to its first three elements.

int[] numbers = { 1, 2, 3 };
int[] newArray = Arrays.copyOf(numbers, 4);

Of course you can reassign the new array to the original variable:

numbers = Arrays.copyOf(numbers, 4);

The copyOf method comes with ten overloads, eight for each type of Java primitives and two for objects. Here are their signatures:

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)

Each of these overloads may throw a java.lang.NullPointerException if original is null and a java.lang.NegativeArraySizeException if newLength is negative.

The newLength argument can be smaller, equal to, or larger than the length of the original array. If it is smaller, then only the first newLength elements will be included in the copy. If it is larger, the last few elements will have default values, i.e. 0 if it is an array of integers or null if it is an array of objects.

Another method similar to copyOf 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)

You can also use System.arraycopy() to copy an array. However, Arrays.copyOf() is easier to use and internally it calls System.arraycopy().