0% found this document useful (0 votes)
10 views11 pages

Java 2D Array

java 2d array

Uploaded by

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

Java 2D Array

java 2d array

Uploaded by

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

Real time problem

How to store values?


A teacher wants to store a subject
marks for every student.
0 1 2 ……………… 119
subject[]
In her class , students count is 120
Real time problem
0 1 2 ……………… 119
A teacher wants to store 5 subject subject1[]
marks for every student.
0 1 2 ……………… 119
In her class , students count is 120 subject2[]

.
.
0 1 2 ……………… 119
subject5[]
Design for large inputs
1. Too many arrays. • Goals
– Arrays count != number of
2. Lets find a solution subjects
• One array for entire set

– Still identify inputs individually


• Like first student’s first
subject mark + second
student’s first subject mark
+…
Design for large inputs
row size = 2;
• Solution:
• Goals column size = 5;
– marks[][] = {
– Arrays count != number of
{21, 24, 25, 28, 32},
subjects
{69, 42, 63, 45, 95}
• One array for entire set
};
– marks
– Still identify inputs individually
• holds the list of numbers
• Like first student’s first
• Type – 2D Array: Indicated by [][]
subject mark + second
– Row size
student’s first subject mark
• Number of subjects in the array
+…
– Column size
• Number of students in the array
Design for large inputs
– marks[][] = { How to access elements in the array???
{21, 24, 25, 28, 32},
First subject first student’s mark - marks[0][0]
{69, 42, 63, 45, 95}
}; First subject second student’s mark – marks[0][1]

row size = 2; Second subject last student’s mark ???


column size = 5; marks[1][4]
Memory Allocation
int arr[][]=new int[2][2];
1000 - 1003 1 marks[0][0]
Syntax:
dataType arrayRefVar[][];
1004 - 1007 2 marks[0][1]
marks[0][0] = 1
1008 - 1011 3 marks[1][0]
marks[0][1] = 2
marks[1][0] = 3
1012 - 1015 4 marks[1][1]
marks[1][1] = 4
How to access ith element and jth element? 1016 - 1019
Row Index = i – 1, Column Index = j - 1 ...
Access it as marks[i - 1][j – 1] ...
Invalid indexing like marks[3][4] or marks[5][0]
a[0]

Memory Allocation a[0][1] a[0][0] 1000


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

a[0]
a[0][2] 1008
a[1]
a a[1] a[2]
a[1][0] 1012
a[0] 4000
1000
a[1][1] 1016
a 4000
a[1] 2000 4004 a[1][2] 1020
5000
a[2] 3000 4008
a[2]
a[2][0] 1024

a[2][1] 1028

a[2][2] 1032
2000 - 2001 1000 a
2D Array
import java.util.Scanner;
public class Main{
1000 - 1003 arr[0][0]
public static void main(String args[]){
1
Scanner s = new Scanner(System.in); 1004 - 1007 2 arr[0][1]
int row = s.nextInt(); // 2
1008 - 1011 3 arr[1][0]
int col = s.nextInt(); // 2
int arr[][] = new int[row][col];
1012 - 1015 4 arr[1][1]
for(i=0; i<row; i++){
for(j=0; j<col; j++){ 1016 - 1019
arr[i][j] = s.nextInt();
1020 - 1023
} ...
}
2000 - 2001 1000 a
2D Array
for(i=0; i<row; i++){
for(j=0; j<col; j++){ 1000 - 1003
1 arr[0][0]
System.out.print(arr[i][j]+ " ");
} 1004 - 1007 2 arr[0][1]
System.out.println();
1008 - 1011 3 arr[1][0]
}
} 1012 - 1015 4 arr[1][1]
}
1016 - 1019

1020 - 1023
...
THANK YOU

You might also like