Skip to main content

Arrays in Java

     By using this array concept, we can store a fixed number of elements of the same data type. There are many ways to declare an array and there are many use cases around this concept. In array, data will be stored starting from 0

Let's see one by one.

Types of array

  1. Single dimensional array
  2. Multidimensional array

1. Single dimensional array

    In this type, the index of the array will be in a linear manner. 

Array declaration

Syntax

DataType[] variable = new DataType[Defined length];
DataType []variable = new DataType[Defined length];
DataType variable[] = new DataType[Defined length];

If the values are known this can be defined as like in below

DataType[] variable = new DataType[]{data 1, data 2, etc.,};
Here the length of the array will depend on the number of data given in the above syntax.

Example

String[] arr = new String[5];
String []arr = new String[] {"a","b"};

In the above example, data will be stored in the below manner
arr[0] = "a"
arr[1] = "b"

Accessing array

    We can access the array using for loop or for each loop. If the program needs any individual item from the array, can access the array using the index of the data.

Example

1. Accessing individual data in an array
    
        String []arr = new String[] {"a","b"};
        System.out.println(arr[1]);
In the above example, the array length is 2 and its index is 0,1. If we try to access an element of index 2, it will throw a runtime exception(java.lang.ArrayIndexOutOfBoundsException).

2. Accessing data using for loop

    Using for loop can able to get array value and can able to access array elements.

Sample program

-----------------------------------------------------------------------------------------------------------------------
public class Array {
public static void main(String[] args) {
String []arr = new String[] {"a","b"};
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
}
}
-----------------------------------------------------------------------------------------------------------------------

Output:
a
b

3. Accessing data using for each

    Using a for-each loop it's been very easy to print all data without giving any index value. It's introduced in JDK 1.5.

-----------------------------------------------------------------------------------------------------------------------
public class Array {
public static void main(String[] args) {
String []arr = new String[] {"a","b"};
for(String element: arr) {
System.out.println(element);
}
}
}
-----------------------------------------------------------------------------------------------------------------------

Output:
a
b

2. Multidimensional Array

    In this type, data will be stored in matrix form. One array index will hold the reference of another array. Based on the requirement add [] to perform multidimensional array.

Array declaration

Syntax

DataType[][] variable = new DataType[Defined length][Defined length];
DataType [][]variable = new DataType[Defined length][Defined length];
DataType variable[][] = new DataType[Defined length][Defined length];

If the values are known this can be defined as like in below

DataType[][] variable = new DataType[][]{{data 1, data 2},{data 1, data 2},....{data n, data n}};
Here the length of the array will depend on the number of data given in the above syntax.

Example

String[] arr = new String[5][5];
String []arr = new String[][] {{"a","b"},{"c","d"}};

In the above example, data will be stored in the below manner

arr[0][0] = "a"
arr[0][1] = "b"
arr[1][0] = "c"
arr[1][1] = "d"

Accessing array

    We can access the array using for loop. If the program needs any individual item from the array, can access the array using the index of the data.

Example

1. Accessing individual data in an array
    
        String[][]arr = new String[][] {{"a","b"},{"c","d"}};
        System.out.println(arr[1][2]);
In the above example, the array with 2 dimensions and both are with length as 2 and its index is {{0,0},{0,1},{1,0},{1,1}}. If we try to access an element of index {2,2}, it will throw a runtime exception(java.lang.ArrayIndexOutOfBoundsException).

2. Accessing data using for loop

    Using for loop can able to get array value and can able to access array elements.

Sample program

-----------------------------------------------------------------------------------------------------------------------
public class Array {
void display(String arr[][]) {
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr.length;j++) {
System.out.println(arr[i][j]);
}
}
}
public static void main(String[] args) {
Array array = new Array();
String[][]arr = new String[][] {{"a","b"},{"c","d"}};
array.display(arr);
}
}
-----------------------------------------------------------------------------------------------------------------------

Output:
a
b
c
d

Useful predefined methods

  1. arrayName.length() - This is used to find the length of an array
  2. Arrays.sort(arrayName) - It sorts the data in the array into ascending order
Hope this will be helpful.
Please give us a suggestion to improve ourselves.

Comments