-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathtwoD_Array.java
76 lines (49 loc) · 1.69 KB
/
twoD_Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
import java.lang.*;
import java.io.*;
class twoDimensionalArray
{
public static void main (String[] args) throws java.lang.Exception
{
int n = 3;
// twoD_Matrix is dynamic two dimensional array initialized with n=3 rows.
ArrayList<ArrayList<Integer> > twoD_Matrix = new ArrayList<ArrayList<Integer>>(n);
//Rows of two-d matrix
ArrayList<Integer> row1 = new ArrayList<Integer>();
ArrayList<Integer> row2 = new ArrayList<Integer>();
ArrayList<Integer> row3 = new ArrayList<Integer>();
//Inserting elements to the row 1
row1.add(10);
row1.add(11);
row1.add(12);
//Inserting elements to the row 2
row2.add(20);
row2.add(21);
row2.add(22);
//Inserting elements to the row 3
row3.add(30);
row3.add(31);
row3.add(32);
//Inserting rows to the matrix
twoD_Matrix.add(row1);
twoD_Matrix.add(row2);
twoD_Matrix.add(row3);
//printing the matrix
for (int i = 0; i < twoD_Matrix.size(); i++) {
// Iterating over each rows of the twoD_Matrix
for (int j = 0; j < twoD_Matrix.get(i).size(); j++) {
//Iterating over each elements of the rows
System.out.print(twoD_Matrix.get(i).get(j) + " ");
}
System.out.println();
}
/*
If user want to give inputs from the console
Scanner input = new Scanner(System.in);
// Instance of scanner class to take input from the console.
System.out.print("Please input no of rows: ");
n=input.nextInt();
Iterate till n and take input from the user and insert into the matrix
*/
}
}