Java Arrays

Java array is a data structure that stores elements of the same type. Arrays contain a sequence of elements. All the elements of an array can be stored in a single variable. For instance there comes a situation, where programmer needs to create a series of elements, then instead of creating individual variable for each element as var1, var2, var3, …, programmer can create an array. That array will contain all elements of the same type.

Java Array
Java Array

Java Array Declaration

Java arrays have four basic attributes, array identifier, array type, array elements and array length. Array name acts as array identifier. Array data type defines the data type of all elements of the array. The syntax for declaring array is:

The best practice is to write square brackets with the data type. Another way of declaring array is similar to declaring arrays in C++:

Array Data Types

Array can have any of the data types defined in Java. The basic data types are given below:

  • int
  • float
  • double
  • short
  • long
  • char
  • byte
  • String

The below code shows how to declare an array of type int:

Java Array Creation

To create an array, the array elements are written in curly braces {}.The syntax for creating an array is:

The below code shows how to create an int array containing three elements:

The elements of String type array are written in double quotes. For example, the below code shows how to create an array of String data type.

Another way of declaring Java array is using new keyword. The syntax for creating an array using new is:

This method creates an array of size given in the square brackets [] by creating a new object of the defined data type. The other syntax for creating array using new is:

Here arraySize is the size of array that can be any integer value greater than 0.

The below code shows how to create an integer array containing three elements.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class MyProgram {
public static void main(String[] args) {
int[] arr = {11, 22, 33};
}
}
public class MyProgram { public static void main(String[] args) { int[] arr = {11, 22, 33}; } }
public class MyProgram {
  public static void main(String[] args) {
    int[] arr = {11, 22, 33};
  }
}

Array Elements

Each element of an array has a unique index. The index identifies the element of array. Array index starts at 0 and increments by 1. Programmer can access individual elements of an array using square braces []. It requires entering desired array index in the square braces to get access the respective element.

In the code below, the program displays elements at different indices of the array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class MyProgram {
public static void main(String[] args) {
String[] colors = {"red", "blue", "green", "yellow"};
System.out.println(colors[0]);
}
}
public class MyProgram { public static void main(String[] args) { String[] colors = {"red", "blue", "green", "yellow"}; System.out.println(colors[0]); } }
public class MyProgram {
  public static void main(String[] args) {
    String[] colors = {"red", "blue", "green", "yellow"};
    System.out.println(colors[0]);
  }
}

The output of the above code after compilation is:

Java programming language also allows the programmer to change individual elements of the array.

The below code assigns a new value to the array element using assignment operator =.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
String[] colors = {"red", "blue", "green", "yellow"};
System.out.println(colors[1]);
colors[1] = "black";
System.out.println(colors[1]);
}
}
public class Main { public static void main(String[] args) { String[] colors = {"red", "blue", "green", "yellow"}; System.out.println(colors[1]); colors[1] = "black"; System.out.println(colors[1]); } }
public class Main {
  public static void main(String[] args) {
    String[] colors = {"red", "blue", "green", "yellow"};
    System.out.println(colors[1]);
    colors[1] = "black";
    System.out.println(colors[1]);
  }
}

The output of the above code after compilation is:

Array Length

An array’s length is equal to the number of elements present in the array. Java provides built in method length() to find the length of the array.

The below code displays the length of array using length() method.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
String[] colors = {"red", "blue", "green", "yellow"};
System.out.println(colors.length);
}
}
public class Main { public static void main(String[] args) { String[] colors = {"red", "blue", "green", "yellow"}; System.out.println(colors.length); } }
public class Main {
  public static void main(String[] args) {
    String[] colors = {"red", "blue", "green", "yellow"};
    System.out.println(colors.length);
  }
}

The output of the above code after compilation is:

Array Traversal

Java programming language allows to traverse an array using loops. Array traversals are used for array manipulations, to get array element as user input and to print arrays. For loop can help achieve array traversal.

The code below shows how to print a java array using for loop.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class MyProgram {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
System.out.println(arr[i]);
}
}
}
public class MyProgram { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { System.out.println(arr[i]); } } }
public class MyProgram {
  public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
      System.out.println(arr[i]);
    }
  }
}

The output of the above code after compilation is:

Another way of array traversal is using for-each loop.

The code below shows how to traverse an array using for-each loop:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class MyProgram {
public static void main(String[] args) {
String[] colors = {"red", "blue", "green", "yellow"};
for (String i : colors) {
System.out.println(i);
}
}
}
public class MyProgram { public static void main(String[] args) { String[] colors = {"red", "blue", "green", "yellow"}; for (String i : colors) { System.out.println(i); } } }
public class MyProgram {
  public static void main(String[] args) {
    String[] colors = {"red", "blue", "green", "yellow"};
    for (String i : colors) {
      System.out.println(i);
    }    
  }
}

In the above code i is the index variable. The loop executes for each string element of the array. Note that for-each loop does not require array length so it is easier in syntax as compared to the for loop. As in for loop, programmer has to define the starting and ending index values. The output of the above code after compilation is:

Multi Dimensional Arrays

So far we have discussed single dimensional arrays but Java also defines multi dimensional arrays like 2D and 3D arrays. Multidimensional arrays have one or more arrays inside a single array. A 2D arrays has two arrays inside an array.

For example, the below code shows how to define a two dimensional array:

The below code shows how to access a single element of a 2D array:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class MyProgram {
public static void main(String[] args) {
int[][] twoArr = { {1, 2, 3, 4}, {5, 6, 7, 8} };
int element = twoArr[1][3];
System.out.println(element);
}
}
public class MyProgram { public static void main(String[] args) { int[][] twoArr = { {1, 2, 3, 4}, {5, 6, 7, 8} }; int element = twoArr[1][3]; System.out.println(element); } }
public class MyProgram {
  public static void main(String[] args) {
    int[][] twoArr = { {1, 2, 3, 4}, {5, 6, 7, 8} };
    int element = twoArr[1][3];
    System.out.println(element);  
  }
}

The output of the above code after compilation is:

In order to traverse multi dimensional arrays, multiple loops are used. The number of loops is equal to the dimensions of the array. For example, two for loops are used to traverse two dimensional arrays.

The below code traverses the two dimensional array using two for loops.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class MyProgram {
public static void main(String[] args) {
int[][] twoArr = { {1, 2, 3, 4}, {5, 6, 7, 8} };
for (int i = 0; i < twoArr.length; ++i) {
for(int j = 0; j < twoArr[i].length; ++j) {
System.out.println(twoArr[i][j]);
}
}
}
}
public class MyProgram { public static void main(String[] args) { int[][] twoArr = { {1, 2, 3, 4}, {5, 6, 7, 8} }; for (int i = 0; i < twoArr.length; ++i) { for(int j = 0; j < twoArr[i].length; ++j) { System.out.println(twoArr[i][j]); } } } }
public class MyProgram {
   public static void main(String[] args) {
     int[][] twoArr = { {1, 2, 3, 4}, {5, 6, 7, 8} };
     for (int i = 0; i < twoArr.length; ++i) {
        for(int j = 0; j < twoArr[i].length; ++j) {
           System.out.println(twoArr[i][j]);
        }
     }
   }
}

The output of the above code after compilation is: