Java Arrays and Multi-dimensional Arrays – Complete Guide with Examples
Learn how to use arrays in Java, including single-dimensional and multi-dimensional arrays, with examples, initialization techniques, and best practices.
Arrays and Multi-dimensional Arrays in Java – Complete Detailed Tutorial
An array is a collection of elements of the same type stored in a contiguous memory location.
Arrays allow us to store multiple values using a single variable.
1. Advantages of Arrays
- Store multiple values in a single variable
- Easy to access elements using index
- Reduce code redundancy
- Support loop-based processing
2. Single-Dimensional Array (1D Array)
2.1 Declaration
datatype[] arrayName;
datatype arrayName[];
Example:
int[] numbers;
String names[];
2.2 Initialization
- Static Initialization
int[] numbers = {10, 20, 30, 40};
- Dynamic Initialization
int[] numbers = new int[5]; // array of size 5
numbers[0] = 10;
numbers[1] = 20;
2.3 Accessing Array Elements
int[] numbers = {10, 20, 30};
System.out.println(numbers[0]); // 10
numbers[1] = 50; // update element
2.4 Iterating Array Using Loop
Example – for loop:
int[] numbers = {10, 20, 30, 40};
for(int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + " is " + numbers[i]);
}
Example – enhanced for loop:
for(int num : numbers) {
System.out.println(num);
}
3. Multi-Dimensional Arrays
- An array of arrays, usually 2D array (rows × columns)
- Can also be 3D or more
3.1 Declaration
datatype[][] arrayName;
datatype arrayName[][];
Example:
int[][] matrix;
3.2 Initialization
- Static Initialization
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
- Dynamic Initialization
int[][] matrix = new int[3][3];
matrix[0][0] = 1;
matrix[1][1] = 5;
3.3 Accessing Elements
System.out.println(matrix[1][2]); // row 1, column 2 → 6
matrix[2][0] = 10; // update element
3.4 Iterating Multi-Dimensional Arrays
Example – 2D array iteration:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for(int i = 0; i < matrix.length; i++) { // rows
for(int j = 0; j < matrix[i].length; j++) { // columns
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Output:
1 2 3
4 5 6
7 8 9
4. Common Array Operations
- Find Length
int[] arr = {10,20,30};
System.out.println(arr.length); // 3
- Copy Array
int[] copy = arr.clone();
- Sort Array
import java.util.Arrays;
Arrays.sort(arr);
- Search in Array
int index = Arrays.binarySearch(arr, 20);
System.out.println(index);
5. Example Program – Arrays in Java
public class ArrayDemo {
public static void main(String[] args) {
// Single-dimensional array
int[] numbers = {10, 20, 30, 40};
System.out.println("1D Array Elements:");
for(int num : numbers) {
System.out.println(num);
}
// Multi-dimensional array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("2D Array Elements:");
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
Sample Output:
1D Array Elements:
10
20
30
40
2D Array Elements:
1 2 3
4 5 6
7 8 9
6. Summary
- Array → collection of elements of same type
- 1D array → single line of elements
- Multi-dimensional array → matrix of elements
- Arrays can be iterated using for, enhanced for, nested loops
- Supports length, sort, search, copy operations