set size of array java code example

Example 1: how to declare array java

int intArray[];    //declaring array
intArray = new int[20];  // allocating memory to array
//OR
int[] intArray = new int[20]; // combining both statements in one

Example 2: java how to change the length of an array

The size of an array cannot be changed after instantiation
If you need to change the size, create a new array and copy the contents
or use an ArrayList which does require a set size

Example 3: set array length java

int[] num = new int[5];

Tags:

Java Example