Resizing an array

An array can’t be resized. You will have to create a larger temporary array,
copy the values and have the original array point to the temporary one.
Take a look at the collection classes to use as an alternative for arrays.

Following app demonstrates the use:

public class Main
{
   public static void main(String args[]) {
      int []values = { 1, 2, 3, 4, 5, 6 };
 
      System.out.println("Before:");
      printArray(values);      
 
      // double the size
      int []temp = new int[values.length * 2];
      System.arraycopy(values, 0, temp, 0, values.length);
      values = temp;
 
      System.out.println("After:");
      printArray(values);      
   }
 
   public static void printArray(int []array) {
      for (int i=0; i<array.length; i++) {
         System.out.print(array[i] + " ");
      }
      System.out.println();
   }
}

outputs:

Before:
1 2 3 4 5 6 
After:
1 2 3 4 5 6 0 0 0 0 0 0