Rahul/multidimensional/pr3 (#6040)

* Making examples simple

* Changing variable names

* Modificatons in naming

* Giving extra spaces
This commit is contained in:
rahusriv 2019-01-02 08:26:43 +05:30 committed by KevinGilmore
parent 9053ecd128
commit 0d5483e1bf
2 changed files with 16 additions and 15 deletions

View File

@ -6,11 +6,11 @@ public class ArrayListOfArrayList {
public static void main(String args[]) { public static void main(String args[]) {
int numVertices = 3; int vertexCount = 3;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(numVertices); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertexCount);
//Initializing each element of ArrayList with ArrayList //Initializing each element of ArrayList with ArrayList
for(int i=0; i< numVertices; i++) { for(int i = 0; i< vertexCount; i++) {
graph.add(new ArrayList<Integer>()); graph.add(new ArrayList<Integer>());
} }
@ -22,12 +22,13 @@ public class ArrayListOfArrayList {
graph.get(2).add(1); graph.get(2).add(1);
graph.get(0).add(2); graph.get(0).add(2);
//Printing all the edges vertexCount = graph.size();
for(int vertexNo=0; vertexNo<numVertices; vertexNo++) { for(int i = 0; i < vertexCount; i++) {
int edgeCount = graph.get(vertexNo).size(); int edgeCount = graph.get(i).size();
ArrayList<Integer> listOfVertices = graph.get(vertexNo); for(int j = 0; j < edgeCount; j++) {
for(int i=0; i<edgeCount; i++) { Integer startVertex = i;
System.out.println("Vertex "+vertexNo+" is connected to vetex "+listOfVertices.get(i)); Integer endVertex = graph.get(i).get(j);
System.out.printf("Vertex %d is connected to vertex %d%n", startVertex, endVertex);
} }
} }
} }