I have an array list of array lists. And I am trying to iterate through them. However I keep getting compilation error. Where am I going wrong. Is there a better way to iterate.
Code:
import java.util.ArrayList;
public class ListofLists {
public static ArrayList family() {
// ArrayList of ArrayLists
ArrayList<ArrayList<String>> couples = new ArrayList<ArrayList<String>>();
ArrayList<String> husbands = new ArrayList<String>();
husbands.add("brad");
husbands.add("jessie");
couples.add(husbands);
ArrayList<String> wives = new ArrayList<String>();
wives.add("jolie");
wives.add("jena");
couples.add(wives);
return couples;
}
public static void main(String[] args) {
ArrayList couples = family();
for (Object couple : couples) {
for (String person: couple) {
System.out.println(person);
}
}
}
}
Compilation Error:
required: array or java.lang.Iterable
found: Object
Expected Output:
brad
jessie
jolie
jena