Please tell me the difference between for loop and iterate? when should i use for/iterate?
ex:-
for(int i=0;i<arrayList.size();i++)
{
System.out.println(arrayList.get(i));
}
// using iterator
Iterator animalItr = animalList.iterator();
while(animalItr.hasNext()) {
String animalObj = (String)animalItr.next();
System.out.println(animalObj);
}
Using an Iterator to iterate through a Collection is the safest and fastest way to traverse through a Collection.
For the collections like ArrayList here, which are array-backed, it might not make a difference.
One of the best reasons for using an Iterator is that you can safely modify the collection without raising a ConcurrentModificationException.