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.
- 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
<K>
void
   Â
iterateUsingForEach(List<List<K> > listOfLists)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Iterate the 2D list
       Â
// and print each element
       Â
System.out.println(
"["
);
Â
ÂÂ Â Â Â Â Â Â Â
for
(List<K> 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<List<Integer> > listOfLists
           Â
=
new
ArrayList<List<Integer> >();
Â
ÂÂ Â Â Â Â Â Â Â
// Create N lists one by one
       Â
// and append to the list of lists
       Â
List<Integer> list1
           Â
=
new
ArrayList<Integer>();
       Â
list1.add(
5
);
       Â
list1.add(
10
);
       Â
listOfLists.add(list1);
Â
ÂÂ Â Â Â Â Â Â Â
List<Integer> list2
           Â
=
new
ArrayList<Integer>();
       Â
list2.add(
1
);
       Â
listOfLists.add(list2);
Â
ÂÂ Â Â Â Â Â Â Â
List<Integer> list3
           Â
=
new
ArrayList<Integer>();
       Â
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, ], ]
- 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
<K>
void
   Â
iterateUsingIterator(List<List<K> > 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<K>
           Â
List<K> list =
new
ArrayList<K>();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
list = (List<K>)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<List<Integer> > listOfLists
           Â
=
new
ArrayList<List<Integer> >();
Â
ÂÂ Â Â Â Â Â Â Â
// Create N lists one by one
       Â
// and append to the list of lists
       Â
List<Integer> list1
           Â
=
new
ArrayList<Integer>();
       Â
list1.add(
5
);
       Â
list1.add(
10
);
       Â
listOfLists.add(list1);
Â
ÂÂ Â Â Â Â Â Â Â
List<Integer> list2
           Â
=
new
ArrayList<Integer>();
       Â
list2.add(
1
);
       Â
listOfLists.add(list2);
Â
ÂÂ Â Â Â Â Â Â Â
List<Integer> list3
           Â
=
new
ArrayList<Integer>();
       Â
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, ], ]