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.
  • 91,686
  • 21
  • 171
  • 177
Scorpion
  • 577
  • 4
  • 21

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
  • 38,363
  • 16
  • 94
  • 159