Arrays

Java array is an object that contains elements of similar datatype since it is an object we cannot use member length to find the array length

Array can be declared like any other variable with datatype and name followed by []

The size of the array should be specified initially in int which cannot be increased during runtime which is identified as one of the setbacks while using an array

Array indices

Array performs index-based operations. An array can be considered as below

Where 0,1,2…. are the indexes of an array. If the size of the array is n the indexes of the array would be counted from n-1

Types of Array

There are two types of array

-> Single dimensional array

-> Multidimensional array

Single dimensional array

One dimensional array represents one row or one column of array elements distinguished by index values

Declaration of an array

An array can be declared in any of the following ways

dataType[] arr; (or) 

dataType []arr; (or) 

dataType arr[]; 

Where datatype can be a primitive data type like int, char, Double, byte, etc. and arr is an identifier representing the array name.

Instantiation of an array

When an array is declared, only a reference of an array is created. To create or give memory to an array, you create an array  below

arr-name = new type [size];

Where arr-name is a reference variable for the array, type specifies the type of data being allocated, size specifies the number of elements in the array

The elements in the array allocated by new will automatically be initialized to the default assigned values of the array as follows

-> boolean :false

-> int : 0

-> double : 0.0

-> String :null

-> User Defined Type :null

Use case demonstrating declaration, instantiation, and initialization of an array

 

public class Samplearray {
    public static void main(String args[]) {
        int x[] = new int[5];//declaration and instantiation 
        x[0] = 10;//initialization 
        x[1] = 20;
        x[2] = 70;
        x[3] = 40;
        x[4] = 50;
        //traversing array 
        for (int i = 0; i < x.length; i++)//length is the property of array 
            System.out.println(x[i]);
    }
}

 

Output:

10
20
70
40
50
 
Iterating an array using each

There is also a "for-each" loop, which is used exclusively to loop through elements in an array

Syntax

for (type variable : arrayname) {
  ...
}

use case demonstrating usage of for each to iterate the array

public class MySample {
    public static void main(String[] args) {
        String[] a = {"x", "y", "z", "o"};
        for (String i : a) {
            System.out.println(i);
        }
    }
}

 

Output:

x
y
z
o
 
Passing and returning an array in a method
We can pass the java array to method and return the same
Use case demonstrating passing array
 
public class Samplearray2 {
    static void test(int arr[]) {
        int test = arr[0];
        for (int i = 1; i < arr.length; i++)
            if (test > arr[i])
                test = arr[i];
        System.out.println(test);
    }
    public static void main(String args[]) {
        int a[] = {1, 3, 7, 5};
        test(a);//passing array to method
    }
}

 

Output:

1
 

Multidimensional array

Multidimensional arrays are arrays with each element of the array holding the reference of another array. The syntax to declare a multidimensional array is as follows

int[][] intArray = new int[10][20]; //a 2D array or matrix

int[][][] intArray = new int[10][20][10]; //a 3D array

Use case demonstrating multidimensional array

public class SampleMultiDimensional {
    public static void main(String args[]) {
        // declaring and initializing 2D array
        int arr[][] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
        // printing 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 0 0
0 1 0
0 0 1

 

Array index out of bound exception

It is one of the common exceptions whenever we are handling an array.JVM throws this exception when the array size is negative or equal to more than the declared size of an array.

 

Related Tutorials

Map