Java Array


An array is an object that contains fixed number of elements of similar type that have contiguous memory locations.

It is an index based data structure where the first element is stored at index 0, second element at index 1 and so on.

We can randomly access any data located at any index directly through its index.

An array contains only fixed number of elements and it doesn’t grow its size at runtime.


Types of Array

There are 2 types of array in java:

  1. Single Dimensional Array
  2. Multidimensional Array

Java Single Dimensional Array


A single dimensional array can only have one full row or one full column.

Syntax:

dataType[] arr;
    or
dataType []arr;
    or
dataType arr[];

Initialisation/creation:

We can instantiate an array by using the new operator with the following syntax:

arr=new datatype[size];

The above statement creates an array using new dataType[arraySize] and assigns the reference of the newly created array to the variable arr.

We can combine the declaration of an array variable, creation of an array and assigning the reference of the array to the variable in one statement as shown below:

dataType[] arr = new dataType[arraySize];

Example:

class ArrayExample
{
    public static void main(String args[])
    {    
        int arr[]={10,20,30,40};
        // length is the property of array

        for(int i=0; i < arr.length; i++)
        {
            // printing all the array elements
            System.out.println(arr[i]);
        }
    }
}

Output:

10
20
30
40

Passing an array to a method

Example:

public static void ArrayExample(int[] array) 
{
    for (int i = 0; i < array.length; i++) 
    {
        System.out.print(array[i] + " ");
    }
}

ArrayExample(new int[]{0,1,2,3,4});
Returning an array from a method
public static int[] ArrayReverse(int[] arrayList) 
{
    int[] reverse = new int[arrayList.length];
    
    for (int i = 0, j = reverse.length - 1; i < arrayList.length; i++, j--) 
    {
        reverse[j] = arrayList[i];
    }
    return reverse;
}

Returning an array from a method

Example:

public static int[] ArrayReverse(int[] arrayList) 
{
    int[] reverse = new int[arrayList.length];
    for (int i = 0, j = reverse.length - 1; i < arrayList.length; i++, j--) 
    {
        reverse[j] = arrayList[i];
    }
    return reverse;
}

Java Multi Dimensional Array


A multidimensional array can have multiple rows and multiple columns as in a matrix.

Syntax:

datatype[][] identifier;
    or
datatype identifier[][];

Initialisation/creation:

The new operator is used to initialise a multi dimensional array.

// 2 row and 2 column
int[][] arr=new int[2][2];

// 2 row and 2 column
int[][] arr={{0,1,2},{3,4,5},{6,7,8}}

We can access a multi dimensional array as arrayName[row][col].

Example:

class ArrayExample
{  
    public static void main(String args[])
    {   
        int arr[][]={{1,2,3},{4,5,6},{7,8,9}}

        // declaring and initializing 2D array
        for(int i = 0; i < 3; i++)
        {
            for(int j=0; j < 3; j++)
            {
                System.out.print(arr[i][j]+" ");
            }

         	System.out.println();  
        }    
    }
}

Output:

1 2 3
4 5 6
7 8 9