0% found this document useful (0 votes)
90 views

2D Array

The document contains information about marks scored by 4 students - David, Michael, Kajal and Salini in 4 subjects. It discusses using individual arrays to store the marks of each student versus using a two dimensional array. It explains that a two dimensional array is an array of arrays that can represent tabular data with rows and columns. It provides an example of declaring and initializing a 2D array in Java. This allows storing the marks of all students using a single 2D array instead of multiple individual arrays, reducing the number of arrays needed.

Uploaded by

Ram Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

2D Array

The document contains information about marks scored by 4 students - David, Michael, Kajal and Salini in 4 subjects. It discusses using individual arrays to store the marks of each student versus using a two dimensional array. It explains that a two dimensional array is an array of arrays that can represent tabular data with rows and columns. It provides an example of declaring and initializing a 2D array in Java. This allows storing the marks of all students using a single 2D array instead of multiple individual arrays, reducing the number of arrays needed.

Uploaded by

Ram Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Real Time Problem

In a class, David obtained 76, 55, 82, 67 marks,


Michael scored 87, 36, 73, 89 marks, kajal scored 75,
45, 96, 32 marks and Salini scored 52, 95, 64, 88 marks
(out of 100) in four different subjects.

Consider the situation, you need to store the marks


obtained by David , Michael, Kajal and Salini.
Solution 1
We can use four individual arrays to get the marks scored by David,
Michael, Kajal and Salini.

Marks scored by
76 55 82 67
David
Marks scored by
87 36 73 89
Michael
Marks scored by Kajal
75 45 96 32

Marks scored by Salini


52 95 64 88
Problem and Solution
Problem
• Usage of too many Array

Expectation
• Should get all the inputs
• Number of arrays should be less HOW?
Two Dimensional Arrays
• It is an Array of Arrays

• Example:
marks[ ][ ] = { {5, 12, 7},
{13, 4, 15} }
Two Dimensional Arrays
• Two subscripts[ ][ ]

• Represents tabular data (rows and columns)

int marks[ ][ ]=new int[2][3];


3 columns

Index [0] [1] [2]


[0] 5 12 7
2 rows
[1] 13 4 15
How to declare Two Dimensional Arrays
Syntax:

Data_Type Array_Name[ ][ ];
Array_Name = new Date_Type[row_size][column_size];

Example:
1. int num[ ][ ];
num = new[3][4];

2. int num[ ][ ] = new int[3][4];


Two Dimensional Arrays
Example:
int a[][]= new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

a[0] a[1] a[2]


How to access ielements
th
elementinand
the jarray?
th
element?
• Row Index = i – 1, Column Index = j – 1
a[0] 1 2 3 • First row first column - a[0][0]
• Access it as a[i - 1][j – 1]
• First row second column – a[0][1]
a[1] 4 5 6
•Invalid
Second row third
indexing column?
like a[3][4] or a[5][0]
7 8 9 It will throw an Exception called ArrayIndexOutofBoundsException
a[1][2]
a[2]
a[0]

Memory Allocation a[0][0] 1000

a[0]
a[0] a[1] a[2]
a[0][1] 1002

a[0][2] 1004 a[1]


a[1] a[2]
a a[1][0] 2000
a[0] 1000 4000
a[1][1] 2002
a 4000
a[1] 2000 4002 a[1][2] 2004
5000 a[2]
a[2] 3000 4004
a[2][0] 3000
a[2][1] 3002
a[2][2] 3004
1 // Predict the output
2 public class Main{
3 public static void main(String args[])
4 {
5 int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6 for(int i=0;i<3;i++)
7 {
8 for(int j=0;j<3;j++)
9 {
10 System.out.print(arr[i][j]+" ");
11 }
12 System.out.println();
13 }
14 }
15 }
16
17
18
19
20
21
22
1 //Predict the Output
2 class Main{
3 public static void printing_2D(int mat[][])
4 {
5 for (int i = 0; i < mat.length; i++){
6 for (int j = 0; j < mat[i].length; j++){
7 System.out.print(mat[i][j] + " ");
8 }
9 System.out.println();
10 }
11 }
12 public static void main(String args[])
13 {
14 int mat[][] = { { 1, 2, 3, 4 },
15 { 5, 6, 7, 8 },
16 { 9, 10, 11, 12 } };
17 printing_2D(mat);
18 }
19 }
20
21
22
Question 1
Write a Java code to print the two dimensional matrix for the given input.

Sample Input: Sample Output:


row = 3 596
column = 3 067
matrix[][] 248
596
067
248
1 import java.util.Scanner;
2 public class Main{
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int row = sc.nextInt();
6 int column = sc.nextInt();
7 int matrix[][] = new int[row][column];
8 for(int i = 0; i < row; i++){
9 for(int j = 0; j < column; j++){
10 matrix[i][j] = sc.nextInt();
11 }
12 }
13 for(int i = 0; i < matrix.length; i++)
14 {
15 for(int j = 0; j < matrix[i].length; j++)
16 {
17 System.out.print(matrix[i][j] + " ");
18 }
19 System.out.println();
20 }
21 }
22 }
Whether it is possible to create an array
with different number of columns in each
row?
YES
BUT HOW?
2D String
Declaration & Initialization
String str[ ][ ] = new String[2][2]{{what, where},{how ,when}} ;

str[ ][0] str[ ][1]


str[0][ ] what
null where
null
str[1][ ] null
how null
when
Question 1
Write a Java code to get n number of string as input and print the string

Sample Input: Sample Output:


n=2 Cat How
Cat Ten Lion
How
Ten
Lion
1 // Predict the output
2 import java.util.*;
3 public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 int n = sc.nextInt();
7 String arr[][] = new String[n][n];
8 for (int i = 0; i < n; i++) {
9 for (int j = 0; j < n; j++) {
10 arr[i][j] = sc.next();
11 }
12 }
13 for (int i = 0; i < n; i++) {
14 for (int j = 0; j < n; j++) {
15 System.out.print( arr[i][j]+" ");
16 }
17 System.out.println();
18 }
19 }
20 }
21
22
MCQ
1 // Predict the output
2 public class Main{
3 public static void main(String args[])
4 {
5 int array[][] = { { 42, 2, 23},
6 { 5, 60, 12},
7 { 19, 10, 1},
8 };
9 function(array);
10 }
11 public static void function(int array[][])
12 {
13 for (int i = 0; i < 3; i++)
14 {
15 for (int j = 0; j < 3; j++)
16 {
17 System.out.print(array[j][i] + " ");
18 }
19 System.out.println();
20 }
21 }
22 }
Question 1
A) 42 5 19
2 60 10
23 12 1

B) 42 2 23
5 60 12
19 10 1
1 // Predict the output
2 public class Main{
3 public static void main(String args[])
4 {
5 int array[][] = { { 1, 2, 3, 4},
6 { 5, 6, 7, 8},
7 { 9, 10, 11, 12},
8 {13, 14, 15, 16}
9 };
10 System.out.print(array[4][4] + " ");
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 2
A) 16

B) Error
1 // Predict the output
2 public class Main{
3 public static void main(String[] args){
4 int arr[][] = new int[2][];
5 arr[0] = new int[3];
6 arr[1] = new int[2];
7
8 int count = 0;
9 for (int i=0; i<arr.length; i++){
10 for(int j=0; j<arr[i].length; j++){
11 arr[i][j] = count++;
12 }
13 }
14 for (int i=0; i<arr.length; i++)
15 {
16 for (int j=0; j<arr[i].length; j++){
17 System.out.print(arr[i][j] + " ");
18 }
19 System.out.println();
20 }
21 }
22 }
Question 3
A) 0 0 0
00

B) 1 2
345

C) 0 1
234

D) 0 1 2
34
THANK YOU

You might also like