Iterate through an ArrayList of ArrayLists in Java

arraylist, iterator, java

Solution

This is the canonical way you do it:

for(List<Integer> innerList : row1) {
    for(Integer number : innerList) {
        System.out.println(number);
    }
}

Problem

I have the following ArrayList... ``` ArrayList<ArrayList<Integer>> row1 = new ArrayList<ArrayList<Integer>>(); ``` The following arraylists are added to it.... ``` row1.add(cell1); row1.add(cell2); row1.add(cell3); row1.add(cell4); row1.add(totalStockCell); ``` I want to iterate through the arraylist row1 and print the contents. Would a loop within a loop work here? E.g. ``` while(it.hasNext()) { //loop on entire list of arraylists while(it2.hasNext) { //each cell print values in list } } ```

Original source