How to append to an array in Java?

To append to an array in Java, first copy the array with Arrays.copyOf(array, array.length + 1), then assign the new value under the array.length index.

Here's how you do it:

int[] array = new int[] { 100, 200, 300 };

int[] newArray = Arrays.copyOf(array, array.length + 1);
newArray[array.length] = 400;

// [100, 200, 300]
System.out.println(java.util.Arrays.toString(array));
// [100, 200, 300, 400]
System.out.println(java.util.Arrays.toString(newArray));