Java Arrays

An array is a structure that holds multiple values of the same type. The length of an array is established when the array is created (at runtime). After creation, an array is a fixed-length structure.
Arrays are defined and used with the square-brackets indexing operator [ ]. To define an array you simply follow your type name with empty square brackets:
int[ ] a1;

You can also put the square brackets after the identifier to produce exactly the same meaning:
int a1[ ];

For arrays, initialization can appear anywhere in your code, but you can also use a special kind of initialization expression that must occur at the point where the array is created. This special initialization is a set of values surrounded by curly braces. The storage allocation (the equivalent of using new) is taken care of by the compiler in this case. For example:
int[] a1 = { 1, 2, 3, 4, 5 };

So why would you ever define an array handle without an array?
int[] a2;

Well, it's possible to assign one array to another in Java, so you can say:
a2 = a1;

Example:

Following statement declares an array variable, myList, creates an array of 10 elements of double type, and assigns its reference to myList.:
double[] myList = new double[10];


Following picture represents array myList. Here myList holds ten double values and the indices are from 0 to 9.

Previous Post Next Post