# Graph Representation using Java ArrayList

Prerequisite : Graph and its representations Following is adjacency list representation of the above graph. The idea is to use ArrayList of ArrayLists.

 `// Java code to demonstrate Graph representation ` `// using ArrayList in Java ` `import` `java.util.*; ` ` `  `class` `Test { ` ` `  `    ``static` `void` `addEdge(ArrayList > adj, ` `                        ``int` `u, ``int` `v) ` `    ``{ ` `        ``adj.get(u).add(v); ` `        ``adj.get(v).add(u); ` `    ``} ` ` `  `    ``static` `void` `printAdjacencyList(ArrayList > adj) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < adj.size(); i++) { ` `            ``System.out.println(``"Adjacency list of "` `+ i); ` `            ``for` `(``int` `j = ``0``; j < adj.get(i).size(); j++) { ` `                ``System.out.print(adj.get(i).get(j) + ``" "``); ` `            ``} ` `            ``System.out.println(); ` `        ``} ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Creating a graph with 5 vertices ` `        ``int` `V = ``5``; ` `        ``ArrayList > adj = ``new` `ArrayList >(V); ` `        ``for` `(``int` `i = ``0``; i < V; i++) ` `            ``adj.add(``new` `ArrayList()); ` ` `  `        ``// Adding edges one by one. ` `        ``addEdge(adj, ``0``, ``1``); ` `        ``addEdge(adj, ``0``, ``4``); ` `        ``addEdge(adj, ``1``, ``2``); ` `        ``addEdge(adj, ``1``, ``3``); ` `        ``addEdge(adj, ``1``, ``4``); ` `        ``addEdge(adj, ``2``, ``3``); ` `        ``addEdge(adj, ``3``, ``4``); ` `        ``printAdjacencyList(adj); ` `    ``} ` `}`

Output:

```Adjacency list of 0
1 4
0 2 3 4
1 3
1 2 4
0 1 3
```

Similar Implementation using a separate Class for Graph.

 `// Java code to demonstrate Graph representation ` `// using ArrayList in Java ` `import` `java.util.*; ` ` `  `class` `Graph { ` `    ``ArrayList > adj; ` `    ``int` `V; ` `    ``Graph(``int` `v) ` `    ``{ ` `        ``V = v; ` `        ``adj = ``new` `ArrayList >(V); ` `        ``for` `(``int` `i = ``0``; i < V; i++) ` `            ``adj.add(``new` `ArrayList()); ` `    ``} ` ` `  `    ``void` `addEdge(``int` `u, ``int` `v) ` `    ``{ ` `        ``adj.get(u).add(v); ` `        ``adj.get(v).add(u); ` `    ``} ` ` `  `    ``void` `printAdjacencyList() ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < adj.size(); i++) { ` `            ``System.out.println(``"Adjacency list of "` `+ i); ` `            ``for` `(``int` `j = ``0``; j < adj.get(i).size(); j++) { ` `                ``System.out.print(adj.get(i).get(j) + ``" "``); ` `            ``} ` `            ``System.out.println(); ` `        ``} ` `    ``} ` `} ` ` `  `class` `Test { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Creating a graph with 5 vertices ` `        ``int` `V = ``5``; ` ` `  `        ``Graph g = ``new` `Graph(V); ` ` `  `        ``// Adding edges one by one. ` `        ``g.addEdge(``0``, ``1``); ` `        ``g.addEdge(``0``, ``4``); ` `        ``g.addEdge(``1``, ``2``); ` `        ``g.addEdge(``1``, ``3``); ` `        ``g.addEdge(``1``, ``4``); ` `        ``g.addEdge(``2``, ``3``); ` `        ``g.addEdge(``3``, ``4``); ` `        ``g.printAdjacencyList(); ` `    ``} ` `}`

Output:

```Adjacency list of 0
1 4
0 2 3 4
1 3
1 2 4
0 1 3
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.