Mega Code Archive

 
Categories / Java Tutorial / Collections
 

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. public class MainClass {   public static void main(String[] args) {     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;   } }