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

  1. Store multiple values in a single variable
  2. Easy to access elements using index
  3. Reduce code redundancy
  4. 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

  1. Static Initialization

int[] numbers = {10, 20, 30, 40};
  1. 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

  1. An array of arrays, usually 2D array (rows × columns)
  2. Can also be 3D or more

3.1 Declaration


datatype[][] arrayName;
datatype arrayName[][];

Example:


int[][] matrix;

3.2 Initialization

  1. Static Initialization

int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
  1. 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

  1. Find Length

int[] arr = {10,20,30};
System.out.println(arr.length); // 3
  1. Copy Array

int[] copy = arr.clone();
  1. Sort Array

import java.util.Arrays;
Arrays.sort(arr);
  1. 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

  1. Array → collection of elements of same type
  2. 1D array → single line of elements
  3. Multi-dimensional array → matrix of elements
  4. Arrays can be iterated using for, enhanced for, nested loops
  5. Supports length, sort, search, copy operations