By Dummy for Dummies
Hey, welcome back! Remember when we made variables of variables and called them Arrays? Today we're talking about Arrays of Arrays, called multidimensional arrays. Think of a 2D array as an array that holds other arrays:
And so on...
Imagine a classroom full of chairs. It's not one long line of chairs (that would be a 1D array), it's rows and columns of them:
To point at one exact chair, you'd say "Row 3, column 2." That's exactly how you access data in a 2D array. Let's write the spell for creating one:
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 the array we want to create. Just like we did in making 1D Arrays and variables.[][] → It tells the program that the datatype is actually 2D Array of Integers rather than just a 1D Array of Integer or a variable of integer.new int[2][3] → Just focus on the numbers inside the square brackets i.e. [2][3]. The first number 2 decides the number of rows (How many 1D arrays in this 2D array). And the second number 3 decides the number of columns (How many indexes per 1D Array).Just like each entry in a 1D array has an index, each 1D array inside a 2D array has its own index too, and each entry inside that 1D array has an index as well. So two index systems are running at once.
To reach every entry, we need nested for loops. The outer loop walks the rows (each 1D array), and the inner loop walks the columns (each entry inside that array).
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; 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(); } } }
Just 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 a 2D array for a 3-by-3 Matrix:
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++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }
Taking input in a 2D array is just like taking input into a 1D array. You just have to access the right index, which can be automated using a for loop. Here is a demonstration:
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(); } }
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.
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[][] A = new int[rows][cols]; int[][] B = new int[rows][cols]; int[][] sum = new int[rows][cols]; 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(); } } 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(); } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { sum[i][j] = A[i][j] + B[i][j]; } } 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(); } }
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.
That's it for 2D Arrays in Java. They're Arrays that manage other Arrays. You can go further into 3D Arrays too, but things get more complex from there. For now, this is enough to cover your syllabus.