No announcement

Forming Arrays

Arrays holds sequential collection of elements of the same type. Each value in an array is accessed in an index manner.

To form an array, we must declare a variable to reference the array and define the data type of the array the variable can reference.


int[] myArray; or

int myArray[];

The first way is the preferred way, the second way comes from C/C++ notion. After declaring the array we have to initialize the array.

int[] myArray = new int[20];

The above statement declares and initializes an array of 20 integers.

The array length need not be constant it can be of length n.

int[] myArray = new int[n];

but once an array has been created; its size cannot be changed. Ofcourse values can be changed.

There is also another way for forming an array with some initial value.

int[] initialArray = {5, 10, 22, 0, 35};