2D Arrays

Java
Agenda
-
Creating 2D Arrays
-
Output
-
Input
-
Problems


Java 2D Arrays are simply arrays or arrays.
Each element is an array itself.
These can be used to store matrices or grids.

2D Arrays - Introduction
class Main {
public static void main(String[] args) {
int [][]arr = new int[3][4];
}
}
To iterate over 2D Arrays, we would be using nested loops.
The outer loop will be used to iterate over the rows and the inner loop will be used to iterate over the columns.

Iterating over 2D Arrays
class Main {
public static void main(String[] args) {
int[][] a = {
{2, 5, 1, 7},
{8, -2, 0, 5},
{-9, -1, 3, 2}
};
int rows = 3;
int cols = 4;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

Input
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = sc.nextInt();
}
}
}
}

Problems!
Let's begin!


Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66

Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
}
}

Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
}
}

Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = sc.nextInt();
}
}
}
}

Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = sc.nextInt();
}
}
int[][] c = new int[rows][cols];
}
}

Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = sc.nextInt();
}
}
int[][] c = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
}

Sum of two matrices
Given two matrices of same size, print their sum.
Sample Input
2 3
1 2 3
4 5 6
10 20 30
40 50 60
Sample Output
11 22 33
44 55 66
class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
matrixRead(a);
matrixRead(b);
int[][] c = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
static void matrixRead(int[][] mat) {
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
mat[i][j] = sc.nextInt();
}
}
}
}

Column wise print
Given a matrix, print it column by column.
Sample Input
1 2 3
4 5 6
Sample Output
1 4
2 5
3 6

Column wise print
Given a matrix, print it column by column.
Sample Input
1 2 3
4 5 6
Sample Output
1 4
2 5
3 6
class Main {
public static void main(String[] args) {
int[][] arr = {
{10, 20, 30, 40},
{50, 60, 70, 80},
{90, 100, 110, 120}
};
int rows = arr.length; // 3
int cols = arr[0].length; // 4
for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
2D Arrays
By Tarun Luthra
2D Arrays
- 2,490