# How to iterate over a 2D list (list of lists) in Java

Given a 2D list, the task is to iterate this 2D list in Java.

2D list (list of lists)
The 2D list refers to a list of lists, i.e. each row of the list is another list.

```[
[5, 10],
[1],
[20, 30, 40]
]
```

Iterate a 2D list: There are two ways of iterating over a list of list in Java.

1. Iterating over the list of lists using loop:
• Get the 2D list to the iterated
• We need two for-each loops to iterate the 2D list successfully.
• In the first for-each loop, each row of the 2D lists will be taken as a separate list
```for (List list : listOfLists)
{ }
```
• In the second for-each loop, each item of the list in each row will be taken separately
```for (K item : list)
{ }
```
• Hence we can do any operation on this item. Here we are printing the item.

Below is the implementation of the above approach:

 `// Java code to demonstrate the concept of ` `// list of lists using loop ` ` `  `import` `java.util.*; ` `public` `class` `List_of_list { ` ` `  `    ``// Iterate the 2D list using loop ` `    ``// and print each element ` `    ``public` `static` ` ``void` `    ``iterateUsingForEach(List > listOfLists) ` `    ``{ ` ` `  `        ``// Iterate the 2D list ` `        ``// and print each element ` `        ``System.out.println(``"["``); ` ` `  `        ``for` `(List list : listOfLists) { ` `            ``System.out.print(``"  ["``); ` ` `  `            ``for` `(K item : list) { ` `                ``System.out.print(``"  "` `                                 ``+ item ` `                                 ``+ ``", "``); ` `            ``} ` `            ``System.out.println(``"], "``); ` `        ``} ` `        ``System.out.println(``"]"``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// List of Lists ` `        ``ArrayList > listOfLists ` `            ``= ``new` `ArrayList >(); ` ` `  `        ``// Create N lists one by one ` `        ``// and append to the list of lists ` `        ``List list1 ` `            ``= ``new` `ArrayList(); ` `        ``list1.add(``5``); ` `        ``list1.add(``10``); ` `        ``listOfLists.add(list1); ` ` `  `        ``List list2 ` `            ``= ``new` `ArrayList(); ` `        ``list2.add(``1``); ` `        ``listOfLists.add(list2); ` ` `  `        ``List list3 ` `            ``= ``new` `ArrayList(); ` `        ``list3.add(``20``); ` `        ``list3.add(``30``); ` `        ``list3.add(``40``); ` `        ``listOfLists.add(list3); ` ` `  `        ``// Iterate the 2D list ` `        ``iterateUsingForEach(listOfLists); ` `    ``} ` `} `

Output:

```[
[ 5,  10, ],
[ 1, ],
[ 20,  30,  40, ],
]
```
2. Iterating over the list of lists using iterator:
• Get the 2D list to the iterated
• We need two iterators to iterate the 2D list successfully.
• The first iterator will iterate each row of the 2D lists as a separate list
```Iterator listOfListsIterator = listOfLists.iterator();
```
• Each row of the 2D list can be obtained with the help of next() method of Iterator
```listOfListsIterator.next();
```

But the next() method returns the Iterator as an Object’s object. Hence we need to cast this returned object into a list.

```list = (List)listOfListsIterator.next();
```
• The second iterator will iterate each item of the list in each row separately
```Iterator eachListIterator = list.iterator();
```
• Hence we can do any operation on this item. Here we are printing the item.

Below is the implementation of the above approach:

 `// Java code to demonstrate the concept of ` `// list of lists using iterator ` ` `  `import` `java.util.*; ` ` `  `class` `List_of_list { ` ` `  `    ``// Iterate the 2D list using Iterator ` `    ``// and print each element ` `    ``public` `static` ` ``void` `    ``iterateUsingIterator(List > listOfLists) ` `    ``{ ` `        ``// Iterator for the 2D list ` `        ``Iterator listOfListsIterator ` `            ``= listOfLists.iterator(); ` ` `  `        ``System.out.println(``"["``); ` `        ``while` `(listOfListsIterator.hasNext()) { ` ` `  `            ``// Type cast next() method ` `            ``// to convert from Object to List ` `            ``List list = ``new` `ArrayList(); ` ` `  `            ``list = (List)listOfListsIterator.next(); ` ` `  `            ``// Iterator for list ` `            ``Iterator eachListIterator ` `                ``= list.iterator(); ` ` `  `            ``System.out.print(``"  ["``); ` `            ``while` `(eachListIterator.hasNext()) { ` ` `  `                ``System.out.print( ` `                    ``"  "` `                    ``+ eachListIterator.next() ` `                    ``+ ``", "``); ` `            ``} ` `            ``System.out.println(``"], "``); ` `        ``} ` `        ``System.out.println(``"]"``); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// List of Lists ` `        ``ArrayList > listOfLists ` `            ``= ``new` `ArrayList >(); ` ` `  `        ``// Create N lists one by one ` `        ``// and append to the list of lists ` `        ``List list1 ` `            ``= ``new` `ArrayList(); ` `        ``list1.add(``5``); ` `        ``list1.add(``10``); ` `        ``listOfLists.add(list1); ` ` `  `        ``List list2 ` `            ``= ``new` `ArrayList(); ` `        ``list2.add(``1``); ` `        ``listOfLists.add(list2); ` ` `  `        ``List list3 ` `            ``= ``new` `ArrayList(); ` `        ``list3.add(``20``); ` `        ``list3.add(``30``); ` `        ``list3.add(``40``); ` `        ``listOfLists.add(list3); ` ` `  `        ``// Iterate the 2D list ` `        ``iterateUsingIterator(listOfLists); ` `    ``} ` `} `

Output:

```[
[ 5,  10, ],
[ 1, ],
[ 20,  30,  40, ],
]
```

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections 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 :

3

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