Java 2D Arrays Programming Basics
Java 2D Arrays Programming Basics
Lecture 11
public
class
TwoDArrays
{
public
static
void
main
(
S
tring
args
[]) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
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
.
nextInt
();
}
}
for
(
i
nt
i
=
0
;
i
<
r
ows
;
i
++) {
for
(
i
nt
j
=
0
;
j
<
c
ols
;
j
++) {
System
.
out
.
print
(
numbers
[
i
]
[
j
]
+
"
"
);
}
System
.
out
.
p
rintln
();
}
}
Apna College
}
public
class
TwoDArrays
{
public
static
void
main
(
S
tring
args
[]) {
Scanner
sc
=
new
Scanner
(
System
.
in
);
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
.
nextInt
();
}
}
int
x
=
sc
.
nextInt
();
for
(
i
nt
i
=
0
;
i
<
r
ows
;
i
++) {
for
(
i
nt
j
=
0
;
j
<
c
ols
;
j
++) {
//compare with x
if
(
numbers
[
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:
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
3. Then we will traverse in the column column_end from row_start to
4. Then we will traverse in the row row_end from column_end to
5. Then we will traverse in the column column_start from row_end to
6. We will do the above steps from 2 to 5 until row_start <= row_end
import
java
.
u
til
.*;
public
class
Arrays
{
public
static
void
main
(
S
tring
args
[]) {
Scanner
sc
=
new
Scanner
(
S
ystem
.
in
);
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
.
nextInt
();
}
}
System
.
out
.
p
rintln
(
"
The Spiral Order Matrix
is : "
);
int
rowStart
=
0
;
Apna College
int
rowEnd
=
n
-
1
;
int
colStart
=
0
;
int
colEnd
=
m
-
1
;
while
(
rowStart
<=
rowEnd
&&
colStart
<=
colEnd
)
{
//1
for
(
int
col
=
c
olStart
;
col
<=
colEnd
;
col
++)
{
System
.
out
.
p
rint
(
matrix
[
rowStart
][
col
]
+
" "
);
}
rowStart
++;
//2
for
(
int
row
=
r
owStart
;
row
<=
rowEnd
;
row
++)
{
System
.
out
.
p
rint
(
matrix
[
row
][
colEnd
]
+
" "
);
}
colEnd
--;
//3
for
(
int
col
=
c
olEnd
;
col
>=
colStart
;
col
--)
{
System
.
out
.
p
rint
(
matrix
[
rowEnd
][
col
]
+
" "
);
}
rowEnd
--;
//4
for
(
int
row
=
r
owEnd
;
row
>=
rowStart
;
row
--)
{
Apna College
System
.
out
.
p
rint
(
matrix
[
row
][
colStart
] +
" "
);
}
colStart
++;
System
.
o
ut
.
p
rintln
();
}
}
}
public
class
Arrays
{
public
static
void
main
(
S
tring
args
[]) {
Scanner
sc
=
new
Scanner
(
S
ystem
.
in
);
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
.
nextInt
();
}
}
Apna College
System
.
out
.
p
rintln
(
"
The transpose is : "
);
for
(
i
nt
j
=
0
;
j
<
m
;j
++) {
for
(
int
i=
0
;
i<
n
;
i
++) {
System
.
out
.
p
rint
(
matrix
[
i
]
[
j
]
+
" "
);
}
System
.
o
ut
.
p
rintln
();
}
}
}
Apna College