List Inside List Iteration in Java

(Last Updated On: August 11, 2016)

List:

Usually we can iterate a list using iterator or foreach loop. Here we are first iterating the mainlist which holds sublists. And the sublists are iterated and printed the values.

Program:

package com.javadomain.in;

import java.util.ArrayList;
import java.util.List;

public class ListInsideListIteration {

public static void main(String[] args) {
List mainList = new ArrayList();
List mobileCompanies = new ArrayList();
List laptopCompanies = new ArrayList();

mobileCompanies.add("Nokia");
mobileCompanies.add("Samsung");
mobileCompanies.add("Apple");

laptopCompanies.add("Dell");
laptopCompanies.add("Lenovo");
laptopCompanies.add("Hp");

mainList.add(mobileCompanies);
mainList.add(laptopCompanies);

for (Object subList : mainList) {
for (Object listValues : (List)subList)
System.out.println(listValues);
}

}

}

 

Output:

Nokia
Samsung
Apple
Dell
Lenovo
Hp

 

 

Thanks for reading this post……….!!!

185 total views, 0 views today

Leave a Reply

Your email address will not be published. Required fields are marked *