how to set the size of an array in java code example

Example 1: how to create an array in java

int[] array1 = new int[5]; //int array length 5
String[] array2 = new String[5] //String array length 5
double[] array3 = new double[5] // Double array length 5

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: how to make a fixed size array in java

dataType[] arrayRefVar = new dataType[arraySize];

Example 4: java get size of array

arrayname.length

Tags:

Java Example