I try to learn java, so please don't criticize too harshly! :-)
I try to use an Iterator to iterate through an ArrayList of ArrayList<Strings>
Code would like this (inspired from OCA Java SE8 Programmer 1 Certification Guide by Mala Gupta):
//Here I try to define an Iterator for nestedArrayList to be able to also remove elements. That is why I don't want to use the enhanced for loop.
Iterator <ArrayList<ArrayList<String>>> it = nestedArrayList.iterator<ArrayList<ArrayList<String>>>();//COMPILATION ERROR
//but Eclipse hits me with a compilation error: Syntax Error on token "(". Expression expected after this token.
//Tried all combinations for the syntax but nothing works. What do I miss?
Thanks, and hope that this forum believes in "There are no stupid questions"...
The interator() method of an ArrayList<ArrayList<String>> instance returns an Iterator<ArrayList<String>> instance, and not a Iterator<ArrayList<ArrayList<String>>> instance. You have one level of indirection too much.
You cannot use a for‑each loop to remove elements from that List, but you probably can with a for loop. That has the advantage of having the Iterator declared as a loop‑local variable, so it goes out of scope faster.