Resize an Array while keeping current elements in Java?

You can’t resize an array in Java. You’d need to either:

  1. Create a new array of the desired size, and copy the contents from the original array to the new array, using java.lang.System.arraycopy(...);
  2. Use the java.util.ArrayList<T> class, which does this for you when you need to make the array bigger. It nicely encapsulates what you describe in your question.
  3. Use java.util.Arrays.copyOf(...) methods which returns a bigger array, with the contents of the original array.

Leave a Comment