Java Multi-Dimensional Arrays

📊 Java Multi-Dimensional Arrays (2D Arrays)

A multi-dimensional array is an array of arrays. The most common type is the 2D array, which is similar to a table or matrix.


✅ 1. Declaration and Initialization

Syntax:

dataType[][] arrayName; // Declaration
arrayName = new dataType[rows][columns]; // Allocation

Or combined:

dataType[][] arrayName = new dataType[3][4]; // 3 rows, 4 columns

Example 1: 2D Array Declaration and Initialization

public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

System.out.println(matrix[0][1]); // 2
System.out.println(matrix[2][2]); // 9
}
}


✅ 2. Looping Through a 2D Array

Using nested for loops:

public class Main {
public static void main(String[] args) {
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

✅ 3. Sum of All Elements in 2D Array

public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int sum = 0;

for (int[] row : matrix) {
for (int value : row) {
sum += value;
}
}

System.out.println("Sum of all elements: " + sum);
}
}

📌 Output:

Sum of all elements: 45

✅ 4. Row-wise Sum

public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

for (int i = 0; i < matrix.length; i++) {
int rowSum = 0;
for (int j = 0; j < matrix[i].length; j++) {
rowSum += matrix[i][j];
}
System.out.println("Sum of row " + i + ": " + rowSum);
}
}
}

📌 Output:

Sum of row 0: 6
Sum of row 1: 15
Sum of row 2: 24

✅ 5. Column-wise Sum

public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int columns = matrix[0].length;

for (int j = 0; j < columns; j++) {
int colSum = 0;
for (int i = 0; i < matrix.length; i++) {
colSum += matrix[i][j];
}
System.out.println("Sum of column " + j + ": " + colSum);
}
}
}

📌 Output:

Sum of column 0: 12
Sum of column 1: 15
Sum of column 2: 18

✅ 6. Real-Life Example: Seat Booking in Theater

public class Main {
public static void main(String[] args) {
boolean[][] seats = new boolean[3][5]; // 3 rows, 5 seats per row

// Book some seats
seats[0][1] = true;
seats[1][3] = true;

for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++) {
System.out.print((seats[i][j] ? "[X]" : "[O]") + " ");
}
System.out.println();
}
}
}

📌 Output:

[O] [X] [O] [O] [O]
[O] [O] [O] [X] [O]
[O] [O] [O] [O] [O]

🧠 Key Points

  • A 2D array is an array of arrays.

  • Use nested loops to traverse rows and columns.

  • Useful for tables, matrices, grids, seating arrangements, etc.

  • Can also be extended to 3D arrays or higher.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *