2D Array
2D Array
Marks scored by
76 55 82 67
David
Marks scored by
87 36 73 89
Michael
Marks scored by Kajal
75 45 96 32
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[ ][ ]
Data_Type Array_Name[ ][ ];
Array_Name = new Date_Type[row_size][column_size];
Example:
1. int num[ ][ ];
num = new[3][4];
a[0]
a[0] a[1] a[2]
a[0][1] 1002
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