Java Arrays

An array is a group of like-typed variables that are referred to by a common name. Array of any type can be created and may have one or more dimensions. Arrays offer a convenient means of grouping related information.

An elements of an array occupy a continuous memory location. A specific element in an array is accessed by its index.  In C++, array elements are indexed starting with 0, so that an array of size N is indexed from 0 to N − 1.

 

One-Dimensional Arrays

A one-dimensional array is, essentially, a list of like-typed variables. The syntax to declare an array is:

type var-name[ ];

The general form of new as it applies to one-dimensional arrays appears as follows:

array-var = new type[size];

e.g.

month_days = new int[10];

This declaration will defines an array a of size 10 i.e a block of 10 consecutive objects named a[0], a[1], a[2], … , a[9]. This can be presented in pictorial form as:

array

Figure: Pictorial representation of an array

The element in array is accessed via index. The notation a[i] refers to (i +1) th element in array.

For example, a[1] refers to second element.

There is a second form that may be used to declare an array:

type[ ] var-name = new type[size];

Here, the square brackets follow the type specifier, and not the name of the array variable.

For example, the following two declarations are equivalent:

int al[] = new int[3];
int[] a2 = new int[3];

 

Array Initialization

After declaring an array, it can be initialized element wise. For example:

int a [10];   //declaration
a[0] = 2 ;
a[1] = 4 ;
a[2] = 1 ;
...​
...​
...
a[8] = 42 ;
a[9] = 17 ;

The pictorial representation after initialization would be like:

array-init

Figure: Array initialization

 An array can be explicitly initialized when it is defined by using an initialiser list.

Initalizer list is a list of values of the appropriate type enclosed in braces and separated by commas.

For example,

int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

If the size of an array with an initialiser list is not specified, the array will automatically be allocated memory to match the number of elements in the list. For example, in above initalization, the size of array month_days will be twelve.

The following program explains more about an array.

// Average values of an array.
class Average {
    public static void main(String args[]) {
        double numbers[] = {10.1, 11.2, 12.3, 13.4, 14.5};
        double result = 0;
        int i;
        for (i = 0; i < 5; i++)
            result = result + numbers[i];
        System.out.println("Average is " + result / 5);
    }
}

The output of the program is:

Average is 12.299999999999999

Here, array numbers with 5 elements is defined and initialized with elements. Each of these element can be accessed via their index starting from 0 to 4 (N – 1 = 5 -1 = 4).

 

Multi-Dimensional Arrays

A two-dimensional array is an array of an array. In similar ways, multi-dimensional array is an array of arrays.

A 2-D array is declared in the same way as 1-D array. The following declaration defines an array with size of 4 X 5.

int matrix [ ] [ ] = new int[4][5];

The following declarations is also equivalent to above:

int[][] matrix= new int[4][5];

The above declaration will defines an array a of size 20 ( 4 X 5)  i.e a block of 20 consecutive objects named a[0][0], a[0][1], a[0][2], … , a[3][3], a[3][4]. This can be presented in pictorial form as:

array-2DFigure: Conceptual representation of 4 X 5 array

A multi-dimensional array is defined using multiple adjacent square brackets, and the elements of the array may be initialised with values enclosed in curly braces.

Here is example that defines 2-D array.

int matrix[3][4] = {
   { 2, 7, 9, 3 },
   { 6, 4, 1, 9 },
   { 2, 6, 4, 3 }
};

This statement defines an array of size 12 ( 3 X 4 ) and initializes the with values.

Here is a program to make the idea more clear about 2D array.

// Demonstrate a two-dimensional array.
class TwoDArray {
    public static void main(String args[]) {
        int twoD[][] = new int[4][5];
        int i, j, k = 0;
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 5; j++) {
                twoD[i][j] = k;
                k++;
            }
        }
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 5; j++)
                System.out.print(twoD[i][j] + " ");
            System.out.println();
        }
    }
}

The output of the program is:

0 1 2 3 4 
5 6 7 8 9 
10 11 12 13 14 
15 16 17 18 19 

Here, array twoD with 20 ( 4X5) elements is defined and initialized with elements. Each of these element can be accessed via their  index as presented in conceptual form. 

 

More…

Arrays are implemented as objects. Because of this, there is a special array attribute,length whch gives the size of an array—that is, the number of elements that an array can hold. All arrays have this variable, and it will always hold the size of the array.

The following program helps to understand more about array and array length.

/**
 * This program demonstrates the length array member.
 */
class Length {
    public static void main(String args[]) {
        int a1[] = new int[10];
        int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
        int a3[] = {4, 3, 2, 1};
        System.out.println("length of a1 is " + a1.length);
        System.out.println("length of a2 is " + a2.length);
        System.out.println("length of a3 is " + a3.length);
    }
}

The output of the program is:

length of a1 is 10
length of a2 is 8
length of a3 is 4

Hence, the instance variable length in array gives the size of array.

Java Break, Continue and return
Java Command-Line Arguments