Arrays in Java

An array is used to store values or data of similar types like integer, float or string. An array is declared using datatype with a square bracket and the array name or the identifier. The following program shows an array  num  of type integer which prints the elements of the array with its index number. Index number of the elements in an array starts with 0.

package com.company;

public class Arrays {
    public static void main(String[] args) {
       int[] num = {1,2,3,4,5};

        System.out.println("First element of the array is:  " +num[0]);
        System.out.println("Second element of the array is: " +num[1]);
        System.out.println("Third element of the array is:  " +num[2]);
        System.out.println("Fourth element of the array is: " +num[3]);
        System.out.println("Fifth element of the array is:  " +num[4]);
    }
}

Output:

First element of the array is:  1
Second element of the array is: 2
Third element of the array is:  3
Fourth element of the array is: 4
Fifth element of the array is:  5

The following program shows the use of for loop to iterate through the elements of the array and prints its value. Here, length property is used to get the size or number of elements of the array.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[] num = {1,2,3,4,5};

        for(int i = 0; i < num.length; i++) {

            System.out.println(num[i]);

        }
    }
}

Output:

1
2
3
4
5

The following program shows the use of for-each loop to iterate through the elements of the array. The syntax of for-each loop is   – “for   variable type of the array   :   array name“.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[] num = {1,2,3,4,5};

        for(int numbers: num) {

            System.out.println(numbers);

        }
    }
}

Output:

1
2
3
4
5

A multidimensional array is an array of arrays. A two dimensional array is an array and contains another array as its elements. The following program shows a 2-D array and prints its fourth element of second array.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[][] num = {{1,2,3,4,5},{11,12,13,14}};

        int number = num[1][3];

        System.out.println(number);

    }
}

Output:

14

The following program shows a for loop to print all the elements of the 2-D array. Here, the first for loop access the arrays inside 2-D array and second for loop access the elements of this arrays, using the length property.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[][] num = {{1,2,3,4,5},{11,12,13,14}};

        for(int i = 0; i < num.length; i++) {
            for(int j = 0; j < num[i].length; j++) {
                
                System.out.println(num[i][j]);

            }
        }
    }
}

Output:

1
2
3
4
5
11
12
13
14

The following program shows the use of for-each loop to access the elements of array. Here, first for-each loop access the arrays inside num and second for-each loop access the elements of individual arrays.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[][] num = {{1,2,3,4,5},{11,12,13,14}};

        for(int [] array: num ) {
            for(int elements: array) {

                System.out.println(elements);

            }
        }
    }
}

Output:

1
2
3
4
5
11
12
13
14

A 3-D array is an array of 2-D arrays. The following program shows a 3-D array and prints the element of second array of 2-D array and third element of its first 1-D array. The index number in the square bracket represents, [1] for the second array of the two 2-D arrays, [0] for the 1st array of its two 1 -D arrays and [2] for its third element. 

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[][][] num = {{{1,2,3,4,5},{11,12,13,14}},{{21,22,23},{31,32}}};

        int number = num[1][0][2];

        System.out.println(number);

    }
}

Output:

23

The following program shows for loop to access the elements of 3-D array. Here, the first for loop access the 2-D arrays of this 3-D array. Second for loop access the 1-D array and third for loop access the elements of the array, using the length property.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[][][] num = {{{1,2,3,4,5},{11,12,13,14}},{{21,22,23},{31,32}}};

        for(int i = 0; i < num.length; i++) {
            for(int j = 0; j < num[i].length; j++) {
                for(int k = 0; k < num[i][j].length; k++){

                    System.out.println(num[i][j][k]);

                }
            }
        }
    }
}

Output:

1
2
3
4
5
11
12
13
14
21
22
23
31
32

The following program shows the use of for-each loop to access the elements of the 3-D array. Here, the first for-each loop access the 2-D arrays inside 3-D array num. The second for-each loop access the 1-D arrays of the 2-D array and third for-each loop access the elements.

package com.company;

public class Arrays {
    public static void main(String[] args) {
        int[][][] num = {{{1,2,3,4,5},{11,12,13,14}},{{21,22,23},{31,32}}};

        for(int [][] a2Darray: num) {
            for(int [] a1Darray: a2Darray) {
                for(int elements: a1Darray){

                    System.out.println(elements);

                }
            }
        }
    }
}

Output:

1
2
3
4
5
11
12
13
14
21
22
23
31
32
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments