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
} }
This is the canonical way you do it:
for(List<Integer> innerList : row1) {
for(Integer number : innerList) {
System.out.println(number);
}
}
for (ArrayList<Integer> list : row1)
{
for (Integer num : list)
{
//doSomething
}
}
Java enhanced-for loops use an iterator behind the scenes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With