Skip to content

Commit 0d5483e

Browse files
rahusrivKevinGilmore
authored andcommitted
Rahul/multidimensional/pr3 (eugenp#6040)
* Making examples simple * Changing variable names * Modificatons in naming * Giving extra spaces
1 parent 9053ecd commit 0d5483e

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

‎core-java-collections-list/src/main/java/com/baeldung/list/multidimensional/ArrayListOfArrayList.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public class ArrayListOfArrayList {
66

77
public static void main(String args[]) {
88

9-
int numVertices = 3;
10-
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(numVertices);
9+
int vertexCount = 3;
10+
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertexCount);
1111

1212
//Initializing each element of ArrayList with ArrayList
13-
for(int i=0; i< numVertices; i++) {
13+
for(int i = 0; i< vertexCount; i++) {
1414
graph.add(new ArrayList<Integer>());
1515
}
1616

@@ -21,13 +21,14 @@ public static void main(String args[]) {
2121
graph.get(1).add(0);
2222
graph.get(2).add(1);
2323
graph.get(0).add(2);
24-
25-
//Printing all the edges
26-
for(int vertexNo=0; vertexNo<numVertices; vertexNo++) {
27-
int edgeCount = graph.get(vertexNo).size();
28-
ArrayList<Integer> listOfVertices = graph.get(vertexNo);
29-
for(int i=0; i<edgeCount; i++) {
30-
System.out.println("Vertex "+vertexNo+" is connected to vetex "+listOfVertices.get(i));
24+
25+
vertexCount = graph.size();
26+
for(int i = 0; i < vertexCount; i++) {
27+
int edgeCount = graph.get(i).size();
28+
for(int j = 0; j < edgeCount; j++) {
29+
Integer startVertex = i;
30+
Integer endVertex = graph.get(i).get(j);
31+
System.out.printf("Vertex %d is connected to vertex %d%n", startVertex, endVertex);
3132
}
3233
}
3334
}

‎core-java-collections-list/src/main/java/com/baeldung/list/multidimensional/ThreeDimensionalArrayList.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public static void main(String args[]) {
1212
ArrayList< ArrayList< ArrayList<String> > > space = new ArrayList<>(x_axis_length);
1313

1414
//Initializing each element of ArrayList with ArrayList< ArrayList<String> >
15-
for(int i=0; i< x_axis_length; i++) {
15+
for(int i = 0; i < x_axis_length; i++) {
1616
space.add(new ArrayList< ArrayList<String> >(y_axis_length));
17-
for(int j =0; j< y_axis_length; j++) {
17+
for(int j = 0; j < y_axis_length; j++) {
1818
space.get(i).add(new ArrayList<String>(z_axis_length));
1919
}
2020
}
@@ -33,9 +33,9 @@ public static void main(String args[]) {
3333
space.get(1).get(1).add(1,"Yellow");
3434

3535
//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++) {
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++) {
3939
System.out.println("Color of point ("+i+","+j+","+k+") is :"+space.get(i).get(j).get(k));
4040
}
4141
}

0 commit comments

Comments
 (0)