
In Java, An array is a collection of items of the same type with a fixed number of items. It contains data items that are all the identical. In other ways we can say that it consists of homogeneous data items. The length of the array is determined when it is created.
Each item in an array is referred to as an element, and the array’s first element is stored at the 0th index, the second at the 1st index, and so on. The index of an array element can be used to access it.
Before going through arrays you can learn the basics of Java programming by clicking here.
Table of Contents
What are the limitations of array in Java?
There is no way to expand or reduce the size of an array once it has been established, depending on your needs. As a result, at the memory level, the array idea is not suggested.
How to declare arrays in Java?
An array is defined with square brackets.
int[] array
Types of Array in Java
- Single Dimensional Array or 1D Array
- Multi Dimensional Array (2D Array and 3D Array)
Single Dimensional Array or 1D Array
Syntax to declare single dimensional or 1D array
int[] array; // Syntax 1
int array[]; // Syntax 2
int []array; // Syntax 3
Array declaration and instantiation in Java
int[] array = new int[5];
Example program of a single dimensional array or 1D array
public class ArrayExample {
public static void main(String args[] ){
int[] array = new int[5];
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
//Iterating array
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
Output: 10 20 30 40 50
Multi Dimensional 2D Array
Syntax to declare a multi dimensional or 2D array
int[][] array; // Syntax 1
int [][]array; // Syntax 2
int array[][]; // Syntax 3
int[] array[]; // Syntax 4
int[] []array; // Syntax 5
int []array[]; // Syntax 6
Example program of a multi dimensional array or 2D array
public class ArrayExample {
public static void main(String args[] ){
int[][] array = new int[2][2];
array[0][0] = 10;
array[0][1] = 20;
array[1][0] = 30;
array[1][1] = 40;
//Iterating array
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Output: 10 20 30 40
Multi dimensional 3D array in Java
Syntax to declare a multi dimensional or 3D array
int[][][] array; // Syntax 1
int array[][][]; // Syntax 2
int [][][]array; // Syntax 3
int[] [][]array; // Syntax 4
int[] array[][]; // Syntax 5
int[] []array[]; // Syntax 6
int[][] []array; // Syntax 7
int[][] array[]; // Syntax 8
int [][]array[]; // Syntax 9
int []array[][]; // Syntax 10
How to Find Array Length in Java
The length property is used to determine how many elements are in an array.
int[] array = {10, 20, 30, 40, 50};
int len = array.length; // Output : 5
Looping through an array with for-each loop
for (data-type variable : array-name) {
...
}
Example program using for-each loop
public class ArrayExample {
public static void main(String args[] ){
int[] array = new int[5];
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
//Iterating array
for (int a : array) {
System.out.println(a);
}
}
}
Output: 10 20 30 40 50
Jagged Array In Java
Jagged arrays are arrays that contain a set of arrays within an array of that type. You may make an array in Java and use other arrays as its elements.
A single-dimensional array or a multi-dimensional array can be stored as a jagged array. Any size array may be stored in a jagged array.
What is an irregular multidimensional array? Jagged arrays are also known as irregular multi-dimensional arrays.
When dealing with a large number of arrays of varying sizes, a jagged array comes in useful.
Jagged array that stores two single dimensional arrays
int arr[][] = new int[2][];
Jagged array that stores a two dimensional array
int arr[][][] = new int[3][][];
Program to create a jagged array with single dimensional array
public class Example {
public static void main(String[] args) {
int arr[][] = new int[2][]; // Jagged Array
arr[0] = new int[2];
arr[1] = new int[4];
arr[0][0] = 10;
arr[0][1] = 20;
arr[1][0] = 11;
arr[1][1] = 12;
arr[1][2] = 13;
arr[1][3] = 14;
System.out.println("Display First Array");
for (int i = 0; i < 2; i++) {
System.out.print(arr[0][i] + " ");
}
System.out.println();
System.out.println("Display Second Array");
for (int i = 0; i < 4; i++) {
System.out.print(arr[1][i] + " ");
}
}
}
Output: Display First Array 10 20 Display Second Array 11 12 13 14