Java Arrays

In Java, arrays are a data structure for storing data of the same kind. In other words, an array is one memory space that stores data of the same type.

One-Dimensional Array

To declare an array, first declare the data type to store in the array, and then declare the array name and the size of the space.

data-type var-name[];
data-type[] var-name;

When declaring an array, you do not need to specify the size of the array space. Declaring an array does not immediately create it in memory. If you only declare it and then use the array, a null reference error occurs. To create an array object, you must create the array with the new statement. We will look at new in detail in the chapter related to class objects.

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

The two preceding statements can be used as one statement.

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

That is, declaration and creation can be performed at the same time in one statement as follows.

int[] a = new int[10];

The following example initializes an array of 3 elements, assigns values, and prints them.

package com.devkuma.tutorial.arry;

public class Array1 {

    public static void main(String[] args) {
        int[] a = new int[3];
        a[0] = 1;
        a[1] = 2;
        a[2] = 3;

        System.out.println("a[0] : " + a[0]);
        System.out.println("a[1] : " + a[1]);
        System.out.println("a[2] : " + a[2]);
    }
}

The following example initializes an array of 3 elements, assigns values at once, and prints them.

package com.devkuma.tutorial.arry;

public class Array2 {

    public static void main(String[] args) {
        int[] a = {1, 2, 3};

        System.out.println("a[0] : " + a[0]);
        System.out.println("a[1] : " + a[1]);
        System.out.println("a[2] : " + a[2]);
    }
}

The following example initializes an array of 3 elements, assigns values at once, and prints them using a loop.

package com.devkuma.tutorial.arry;

public class Array3 {

    public static void main(String[] args) {
        int[] a = { 1, 2, 3 };

        for (int i = 0; i < a.length; i++) {
            System.out.println("a[" + i + "] : " + a[i]);
        }
    }
}

The array length can be obtained with length, as in the example above.

Multidimensional Array

A multidimensional array is implemented by using multiple one-dimensional arrays. In other words, after declaring a one-dimensional array, a new array is assigned inside each index. Declaring a multidimensional array is similar to declaring a one-dimensional array.

data-type var-name[][] = new data-type[size][size];
data-type[][] var-name = new data-type[size][size];
data-type[] var-name[] = new data-type[size][size];
int a[][] = new int[4][5];
int[][] a = new int[4][5];
int[] a[] = new int[4][5];

Index values in an array with rows and columns

[0][0] [0][1] [0][2] [0][3] [0][4]
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
[3][0] [3][1] [3][2] [3][3] [3][4]

The right index determines the column, and the left index determines the row.