0% found this document useful (0 votes)
3 views7 pages

Java 2D Arrays Programming Basics

The document provides an introduction to 2D arrays in Java, including how to create, input, and print elements of a 2D array. It also discusses searching for an element in a matrix and includes a homework problem on printing a spiral order matrix. The document contains code snippets to illustrate these concepts.

Uploaded by

Fatima Ahmed
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)
3 views7 pages

Java 2D Arrays Programming Basics

The document provides an introduction to 2D arrays in Java, including how to create, input, and print elements of a 2D array. It also discusses searching for an element in a matrix and includes a homework problem on printing a spiral order matrix. The document contains code snippets to illustrate these concepts.

Uploaded by

Fatima Ahmed
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

‭Java - Introduction to Programming‬

‭Lecture 11‬

‭2D Arrays In Java‬

‭It is similar to 2D matrices that we studied in 11th and 12th class.‬

‭a.‬ ‭Creating a 2D Array - with‬‭new‬‭keyword‬


int‬
‭ [][] marks =‬‭
‭ new int‬
[‭
‭3
‬‬
][‬
‭ 3‬
‭ ]‭
‭;
‬‬

‭b.‬ ‭Taking a matrix as an input and printing its elements.‬


import‬‭
‭ java‬
.‭
‭ u
‬til‬
.*;‬

public‬‭
‭ class‬‭
TwoDArrays‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬‭
args‬
[]) {‬

Scanner‬‭
‭ sc‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭i‬n‬
);‬

int‬‭
‭ rows‬‭
=‬‭
sc‬
.‭
‭ n
‬extInt‬
();‬

int‬‭
‭ cols‬‭
=‬‭
sc‬
.‭
‭ n
‬extInt‬
();‬

int‬
‭ [][]‬‭
‭ numbers‬‭
=‬‭
new‬‭
int‬
[‭
‭ r
‬ows‬
][‬
‭ cols‬
‭ ];‬

//input‬

//rows‬

for‬
‭ (‭
‭ i
‬nt‬‭
i‭
=‬‭
0
‬‭
;
‬‬‭
i‭
<‬‭
r
‬ows‬
;‬‭
‭ i‭
+‬+) {‬
//columns‬

for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=‬‭
0
‬‭
;‬‬‭
j‭
<‬‭
c
‬ols‬
;‬‭
‭ j‭
+‬+) {‬
numbers‬
‭ [‭
‭i
‬‬][‬
‭ j‭
‭]
‬ =‬‭
sc‬
.‭
‭n‬extInt‬
();‬

}‬

}‬

for‬
‭ (‭
‭ i
‬nt‬‭
i‭
=‬‭
0
‬‭
;
‬‬‭
i‭
<‬‭
r
‬ows‬
;‬‭
‭ i‭
+‬+) {‬
for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=‬‭
0
‬‭
;‬‬‭
j‭
<‬‭
c
‬ols‬
;‬‭
‭ j‭
+‬+) {‬
System‬
‭ .‭
‭o‬ut‬
.‬
‭ print‬
‭ (‭
‭n‬umbers‬
[‭
‭i‬‭
]
‬[‬
j‭
‭ ]
‬+‬
"‬‭
‭ "‭
)‬;‬
}‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rintln‬
();‬

}‬

}‬

‭Apna College‬
}‬

‭c.‬ ‭Searching for an element x in a matrix.‬


import‬‭
‭ java‬
.‭
‭ u
‬til‬
.*;‬

public‬‭
‭ class‬‭
TwoDArrays‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬‭
args‬
[]) {‬

Scanner‬‭
‭ sc‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‭
‭i‬n‬
);‬

int‬‭
‭ rows‬‭
=‬‭
sc‬
.‭
‭ n
‬extInt‬
();‬

int‬‭
‭ cols‬‭
=‬‭
sc‬
.‭
‭ n
‬extInt‬
();‬

int‬
‭ [][]‬‭
‭ numbers‬‭
=‬‭
new‬‭
int‬
[‭
‭ r
‬ows‬
][‬
‭ cols‬
‭ ];‬

//input‬

//rows‬

for‬
‭ (‭
‭ i
‬nt‬‭
i‭
=
‬‭
0‬‭
;
‬‬‭
i‭
<‬‭
r
‬ows‬
;‬‭
‭ i‭
+‬+) {‬
//columns‬

for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=
‬‭
0‬‭
;
‬‬‭
j‭
<
‬‭
c
‬ols‬
;‬‭
‭ j‭
+‬+) {‬
numbers‬
‭ [‭
‭i‬‬
][‬
‭ j‭
‭]‬ =‬‭
sc‬
.‭
‭n‬extInt‬
();‬

}‬

}‬

int‬‭
‭ x‬‭
=‬‭
sc‬
.‭
‭n‬extInt‬
();‬

for‬
‭ (‭
‭ i
‬nt‬‭
i‭
=
‬‭
0‬‭
;
‬‬‭
i‭
<‬‭
r
‬ows‬
;‬‭
‭ i‭
+‬+) {‬
for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=
‬‭
0‬‭
;
‬‬‭
j‭
<
‬‭
c
‬ols‬
;‬‭
‭ j‭
+‬+) {‬
//compare with x‬

if‬
‭ (‭
‭n‬umbers‬
[‭
‭ i
‬‭
]‬[‬
j‬
‭ ] ==‬‭
‭ x‭
)
‬ {‬
System‬
‭ .‭
‭o
‬ut‬
.‬
‭ println‬
‭ (‭
‭"‬x found at‬‭
location ("‬‭
+‬‭
i‬‭
+‬‭
", "‬‭
+‬‭
j‬‭
+‬
")"‬
‭ );‬

}‬

}‬

}‬

}‬

}‬

‭Apna College‬
‭Homework Problems‬
‭1.‬ P
‭ rint the spiral order matrix as output for a given matrix of numbers.‬
‭[Difficult for Beginners]‬

‭APPROACH‬‭:‬

‭Algorithm: (We are given a 2D matrix of n X m ).‬


‭1. We will need 4 variables:‬

‭a. row_start - initialized with 0.‬

‭b. row_end - initialized with n-1.‬

‭c. column_start - initialized with 0.‬

‭d. column_end - initialized with m-1.‬

‭2. First of all, we will traverse in the row row_start from column_start‬

‭Apna College‬
‭to column_end and we will increase the row_start with 1 as we have‬

‭traversed the starting row.‬

‭3. Then we will traverse in the column column_end from row_start to‬

‭row_end and decrease the column_end by 1.‬

‭4. Then we will traverse in the row row_end from column_end to‬

‭column_start and decrease the row_end by 1.‬

‭5. Then we will traverse in the column column_start from row_end to‬

‭row_start and increase the column_start by 1.‬

‭6. We will do the above steps from 2 to 5 until row_start <= row_end‬

‭and column_start <= column_end.‬

import‬‭
‭ java‬
.‭
‭ u
‬til‬
.*;‬

public‬‭
‭ class‬‭
Arrays‬‭
{‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬‭
args‬
[]) {‬

Scanner‬‭
‭ sc‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭ S
‬ystem‬
.‭
‭i‬n‬
);‬

int‬‭
‭ n‬‭
=‬‭
sc‬
.‭
‭n
‬extInt‬
();‬

int‬‭
‭ m‬‭
=‬‭
sc‬
.‭
‭n
‬extInt‬
();‬

int‬‭
‭ matrix‬
[][] =‬‭
‭ new‬‭
int‬
[‭
‭ n
‬‭
]‬[‬
m‬
‭ ];‬

for‬
‭ (‭
‭ i
‬nt‬‭
i‭
=‬‭
0
‬‭
;
‬‬‭
i‭
<‬‭
n
‬‭
;‬‬‭
i‭
+‬+) {‬

for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=‬‭
0
‬‭
;‬‬‭
j‭
<‬‭
m
‬‭
;
‬‬‭
j‬++) {‬

matrix‬
‭ [‭
‭i‬‭
]
‬[‬
j‭
‭ ]
‬ =‬‭
sc‬
.‭
‭n‬extInt‬
();‬

}‬

}‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rintln‬
(‭
‭ "
‬The Spiral Order Matrix‬‭
is : "‬
);‬

int‬‭
‭ rowStart‬‭
=‬‭
0‭
;‬‬

‭Apna College‬
int‬‭
‭ rowEnd‬‭
=‬‭
n‭
-‬‭
1
‬‭
;‬‬

int‬‭
‭ colStart‬‭
=‬‭
0‭
;‬‬

int‬‭
‭ colEnd‬‭
=‬‭
m‭
-‬‭
1
‬‭
;‬‬

//To print spiral order matrix‬


while‬
‭ (‭
‭r‬owStart‬‭
<=‬‭
rowEnd‬‭
&&‬‭
colStart‬‭
<=‬‭
colEnd‬
)‬‭
‭ {‬

//1‬

for‬
‭ (‬
‭ int‬‭
‭ col‬
=‭
‭ c
‬olStart‬
;‬‭
‭ col‬
<=‬
‭ colEnd‬
‭ ;‬‭
‭ col‬
++)‬‭
‭ {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭m‬atrix‬
[‭
‭r‬owStart‬
][‬
‭ col‬
‭ ]‬‭
‭ +‬‭
" "‬
);‬

}‬

rowStart‬
‭ ++;‬

//2‬

for‬
‭ (‬
‭ int‬‭
‭ row‬
=‭
‭ r
‬owStart‬
;‬‭
‭ row‬
<=‬
‭ rowEnd‬
‭ ;‬‭
‭ row‬
++)‬‭
‭ {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭m‬atrix‬
[‭
‭r‬ow‬
][‬
‭ colEnd‬
‭ ]‬‭
‭ +‭
"‬ "‬
);‬

}‬

colEnd‬
‭ --;‬

//3‬

for‬
‭ (‬
‭ int‬‭
‭ col‬
=‭
‭ c
‬olEnd‬
;‬‭
‭ col‬
>=‬
‭ colStart‬
‭ ;‬‭
‭ col‬
--)‬‭
‭ {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭m‬atrix‬
[‭
‭r‬owEnd‬
][‬
‭ col‬
‭ ]‬‭
‭ +‬‭
" "‬
);‬

}‬

rowEnd‬
‭ --;‬

//4‬

for‬
‭ (‬
‭ int‬‭
‭ row‬
=‭
‭ r
‬owEnd‬
;‬‭
‭ row‬
>=‬
‭ rowStart‬
‭ ;‬‭
‭ row‬
--)‬‭
‭ {‬

‭Apna College‬
System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭m‬atrix‬
[‭
‭r‬ow‬
][‬
‭ colStart‬
‭ ] +‬‭
‭ " "‬
);‬

}‬

colStart‬
‭ ++;‬

System‬
‭ .‭
‭o
‬ut‬
.‭
‭ p
‬rintln‬
();‬

}‬

}‬

}‬

‭2.‬ ‭For a given matrix of N x M, print its transpose.‬


import‬‭
‭ java‬
.‭
‭ u
‬til‬
.*;‬

public‬‭
‭ class‬‭
Arrays‬‭
{‬

public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬‭
args‬
[]) {‬

Scanner‬‭
‭ sc‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭ S
‬ystem‬
.‭
‭i‬n‬
);‬

int‬‭
‭ n‬‭
=‬‭
sc‬
.‭
‭n
‬extInt‬
();‬

int‬‭
‭ m‬‭
=‬‭
sc‬
.‭
‭n
‬extInt‬
();‬

int‬‭
‭ matrix‬
[][] =‬‭
‭ new‬‭
int‬
[‭
‭ n
‬‭
]‬[‬
m‬
‭ ];‬

for‬
‭ (‭
‭ i
‬nt‬‭
i‭
=‬‭
0
‬‭
;
‬‬‭
i‭
<‬‭
n
‬‭
;‬‬‭
i‭
+‬+) {‬

for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=‬‭
0
‬‭
;‬‬‭
j‭
<‬‭
m
‬‭
;
‬‬‭
j‬++) {‬

matrix‬
‭ [‭
‭i‬‭
]
‬[‬
j‭
‭ ]
‬ =‬‭
sc‬
.‭
‭n‬extInt‬
();‬

}‬

}‬

‭Apna College‬
System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rintln‬
(‭
‭ "
‬The transpose is : "‬
);‬

//To print transpose‬


for‬
‭ (‭
‭ i
‬nt‬‭
j‭
=‬‭
0
‬‭
;
‬‬‭
j‭
<‬‭
m
‬‬‭
;‬j‭
‭+‬+) {‬

for‬
‭ (‬
‭ int‬‭
‭ i‬=‭
‭0‬‭
;
‬‬‭
i‬<‭
‭n‬‭
;
‬‬‭
i‭
+‬+) {‬

System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭m‬atrix‬
[‭
‭i‬‭
]
‬[‬
j‭
‭ ]
‬+‬
" "‬
‭ );‬

}‬

System‬
‭ .‭
‭o
‬ut‬
.‭
‭ p
‬rintln‬
();‬

}‬

}‬

}‬

‭Apna College‬

You might also like