16 Java Fundamentals | Multidimensional Arrays in Java | By Dummy for Dummies
INTRODUCTION:
Hey guys welcome back! Remember we made variable of variables? And called it Arrays? Today we are gonna talk about we can even make Array of Arrays, they are called multidimensional Arrays. Think of a 2D array as an array that contain arrays:
⦿ 2D Array → Contains Arrays → Contains variables/data
And a 3D array as, array that contain arrays, each of those array contain more arrays.
⦿ 3D Array → Contains 2D Arrays → Contains Arrays → Contains variables/data
And so on...
2D ARRAYS
Imagine a classroom full of chairs. It does not contain a single line of chairs (1D Array) but rather rows and columns of chairs. Where:
⦿ Row = Line of chair
⦿ Column = Specific chair in that row
So to point a specific chair you'd say "Row 3, column 2". That is how exactly you can access data in 2D Arrays. Let's first write the java magical spell for creation of 2D Array
int[][] numbers = new int[2][3];
Let's break down the java spell into our human language...
int → It tells the program about the datatype of array we want to create. Just like we did in making of 1D Arrays and variables.
[][] → It tells the program that the datatype is actually 2D Array of Integers rather than just 1D Array of Integer or variable of integer.
new int[2][3] → Just focus on the numbers inside square brackets i.e. [2][3] . The first number 2 decides number of rows (How many 1D arrays in this 2D array). And the second number 3 decides number of columns (How many indexes per 1D Array).
Just like in 1D arrays each entry had specific index, here in 2D array each 1D array also has specific index. And in each 1D array each entry has specific index. So here two systems of indexes run. To access each entry in 2D array we will need nested for loops. Outer loop will be responsible for checking the row (specific 1D array) and inner loop will be responsible for checking column (specific index in that Array).
Let's write a small code in which we will be creating Array and filling it with data, and accessing each step by step
CODE:
public class Main {
public static void main(String[] args) {
// Create a 2x3 2D array (2 rows, 3 columns)
int[][] numbers = new int[2][3];
// Fill the 2D array
numbers[0][0] = 1;
numbers[0][1] = 2;
numbers[0][2] = 3;
numbers[1][0] = 4;
numbers[1][1] = 5;
numbers[1][2] = 6;
// Print the 2D array using nested loops
System.out.println("2D Array elements:");
for (int row = 0; row < numbers.length; row++) {
for (int col = 0; col < numbers[row].length; col++) {
System.out.print(numbers[row][col] + " ");
}
System.out.println();
}
}
}
OUTPUT:
DIRECT INITIALIZATION
Taking inJust like 1D arrays we can also directly initialize 2D arrays. But here again they are not that much useful. Here is the syntax to initialize 2D arrays for 3-by-3 Matrix
CODE:
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print
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();
}
}
}
OUTPUT:
INPUT IN 2D ARRAY
Taking input in 2D array is just like taking input into 1D. You just have to access the right index which can be automated using a for loop. Here is demonstration:
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns: ");
int cols = sc.nextInt();
int[][] data = new int[rows][cols];
System.out.println("Enter elements:");
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
System.out.print("Enter element at [" + r + "][" + c + "]: ");
data[r][c] = sc.nextInt();
}
}
System.out.println("\nYou entered:");
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
System.out.print(data[r][c] + " ");
}
System.out.println();
}
sc.close();
}
}
OUTPUT:
EXERCISE
Let's do an exercise for what we studied in this post. Study this code, break it down, analyze it and identify the concepts used here. It would be even better if you write down your observations and make a mini report on it.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input size
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns: ");
int cols = sc.nextInt();
int[][] A = new int[rows][cols];
int[][] B = new int[rows][cols];
int[][] sum = new int[rows][cols];
// Input first matrix
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter element at [" + i + "][" + j + "]: ");
A[i][j] = sc.nextInt();
}
}
// Input second matrix
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter element at [" + i + "][" + j + "]: ");
B[i][j] = sc.nextInt();
}
}
// Calculate sum
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}
// Print result
System.out.println("\nResultant Matrix (A + B):");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
sc.close();
}
}
OUTPUT:
MINI PROJECT (Monkey’s Matrix Power)
Boss Monkey has two banana matrices. He wants to multiply them element-by-element (not normal matrix multiplication, but multiplying each corresponding element). Then, he wants to find the total sum of the new matrix and also its average.
Tasks 1. Write a program to:
2. Ask the user for the size of the matrix (rows and columns).
3. Take input for two matrices A and B.
4. Multiply them element by element → store result in C.
5. Find the sum of all elements of C.
6. Find the average of elements of C.
7. Print results neatly.
EXAMPLE OUTPUT:
CLOSING That's it for 2D Arrays in Java. They are like Arrays that manage 1D Arrays. You can even create 3D Arrays but that becomes more complex. For now they are enough and covers your syllabus.