Skip to content

Commit 6acf513

Browse files
rahusrivKevinGilmore
authored andcommitted
Multidimentional ArrayList (eugenp#5795)
* Multidimentional ArrayList * Adding code for 3-D ArrayList * Removing comment * Fixing indentation * Spellcheck
1 parent c7da8f3 commit 6acf513

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.list.multidimensional;
2+
3+
import java.util.ArrayList;
4+
5+
public class ArrayListOfArrayList {
6+
7+
public static void main(String args[]) {
8+
9+
int vertex = 5;
10+
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertex);
11+
12+
//Initializing each element of ArrayList with ArrayList
13+
for(int i=0; i< vertex; i++) {
14+
graph.add(new ArrayList<Integer>());
15+
}
16+
17+
//We can add any number of columns to each row
18+
graph.get(0).add(1);
19+
graph.get(0).add(5);
20+
graph.get(1).add(0);
21+
graph.get(1).add(2);
22+
graph.get(2).add(1);
23+
graph.get(2).add(3);
24+
graph.get(3).add(2);
25+
graph.get(3).add(4);
26+
graph.get(4).add(3);
27+
graph.get(4).add(5);
28+
29+
//Printing all the edges
30+
for(int i=0; i<vertex; i++) {
31+
for(int j=0; j<graph.get(i).size(); j++) {
32+
System.out.println("Edge between vertex "+i+"and "+graph.get(i).get(j));
33+
}
34+
}
35+
}
36+
37+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.list.multidimensional;
2+
3+
import java.util.ArrayList;
4+
5+
public class ThreeDimensionalArrayList {
6+
7+
public static void main(String args[]) {
8+
9+
int x_axis_length = 2;
10+
int y_axis_length = 2;
11+
int z_axis_length = 2;
12+
ArrayList< ArrayList< ArrayList<String> > > space = new ArrayList<>(x_axis_length);
13+
14+
//Initializing each element of ArrayList with ArrayList< ArrayList<String> >
15+
for(int i=0; i< x_axis_length; i++) {
16+
space.add(new ArrayList< ArrayList<String> >(y_axis_length));
17+
for(int j =0; j< y_axis_length; j++) {
18+
space.get(i).add(new ArrayList<String>(z_axis_length));
19+
}
20+
}
21+
22+
//Set Red color for points (0,0,0) and (0,0,1)
23+
space.get(0).get(0).add("Red");
24+
space.get(0).get(0).add("Red");
25+
//Set Blue color for points (0,1,0) and (0,1,1)
26+
space.get(0).get(1).add("Blue");
27+
space.get(0).get(1).add("Blue");
28+
//Set Green color for points (1,0,0) and (1,0,1)
29+
space.get(1).get(0).add("Green");
30+
space.get(1).get(0).add("Green");
31+
//Set Yellow color for points (1,1,0) and (1,1,1)
32+
space.get(1).get(1).add("Yellow");
33+
space.get(1).get(1).add("Yellow");
34+
35+
//Printing colors for all the points
36+
for(int i=0; i<x_axis_length; i++) {
37+
for(int j=0; j<y_axis_length; j++) {
38+
for(int k=0; k<z_axis_length; k++) {
39+
System.out.println("Color of point ("+i+","+j+","+k+") is :"+space.get(i).get(j).get(k));
40+
}
41+
}
42+
}
43+
}
44+
45+
}

0 commit comments

Comments
 (0)