If I create an Iterator object on any List, do I have to create a new iterator object every time I traverse the list? Or do new items added or removed from the list automatically get accounted for by the previously created iterator?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Iteratoris a one-use utility. You generally need a new one each time you iterate through the list. However, that’s not a bad thing;Iterators have very little state (for anArrayList, it might be just an int to hold the current index).If the backing list is modified in between iterator calls, you will get a
ConcurrentModificationExceptionfrom the iterator operation. Adding or removing items must be done using theIteratoritself.