0

When I obtain iterator over an ArrayList and use the method next() on it, does it return only next element or it return and remove elements from the iterator. like

Iterator i = list.iterator();
while(i.hasNext())
    System.out.print(i.next()+" ");
Alexis C.
  • 87,500
  • 20
  • 164
  • 172
Scorpion
  • 567
  • 4
  • 19

1 Answers1

5

Calling next() just moves a pointer to the next element
and returns the element. It does not remove any elements.

For more details see here.

http://en.wikipedia.org/wiki/Iterator#Java

http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

Iterators also have a remove() method. That method removes elements.

peter.petrov
  • 36,377
  • 12
  • 75
  • 137