1

I have read about iterator.next() in oracle doc and it says: iterator.next() returns the next element in the iteration. I have this code, suppose the collection is not empty:

while ( iterator.hasNext() ){
        Card nextCard = iterator.next();
        System.out.println( nextCard );
    }

In the first loop, hasNext() is called and there is a pointer points to the first element, then next() is called and return the next element, it means the second element and then move the pointer to the second element, but why can i still print out the first element ?

Thuan Nguyen
  • 431
  • 6
  • 18
  • 2
    The pointer starts by pointing at the first element, so the first call to `next()` returns the first element, etc. – Tim Biegeleisen Aug 28 '18 at 04:24
  • You can still print the first element because your local variable `nextCard` points to it. This has nothing to do with the position of the iterator. – Henry Aug 28 '18 at 04:25
  • @TimBiegeleisen i means at first the pointer points to the first element (index 0), and next() returns the next element as the documents says, so it should return the second element (index 1)? – Thuan Nguyen Aug 28 '18 at 04:38
  • 1
    `next()` picks out and remembers the element at the pointer, _then_ advances the pointer, then returns the _remembered_ element. – Kevin Anderson Aug 28 '18 at 04:49
  • @KevinAnderson So it means that next() returns current element pointed to by a pointer then move the pointer to the next element. But why the document says that it returns the next element? If it returns the next element it should move the pointer to the next element then return it? – Thuan Nguyen Aug 28 '18 at 04:54
  • 1
    You're overthinking it. `next()` returns the next element in the sequence, starting with the first element. – shmosel Aug 28 '18 at 05:02
  • 1
    Well, what is the "next" element when you haven't yet retrieved _any_ elements? It's the first element, yes? – Kevin Anderson Aug 28 '18 at 05:02
  • @KevinAnderson I am really confused because English is not my mother tongue, the document says next() returns the next element but in stead, it returns the current element so it sounds counter-intuitive for me. – Thuan Nguyen Aug 28 '18 at 05:50
  • What word would you use instead? – shmosel Aug 29 '18 at 05:20
  • hasNext() checks its prupose but doesn't move at all, you might wonder the pointer is still not on the first element; so the first usage of next() moves the pointer from where it is to the next element which the first time will return the first element. Consider seeing an iterator as it is : a pointer apart (different from) the list to iterate on. – levolutionniste Sep 17 '20 at 09:40

3 Answers3

0

Documentation says "hasNext() Returns true if the collection has more elements for iteration."

hasNext() does not move the pointer to the next element, it just tells you whether an element is present for next iteration or not. next() in turn returns the next element and moves the pointer.

S.K.
  • 3,597
  • 2
  • 16
  • 31
0

Good Practice to visit Java Docs for clearing out uncertainities. Here is what JavaDoc for Iterator -> hasNext says

boolean hasNext() Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.) Returns: true if the iteration has more elements

hasNext just finds if an element is present at the next index position and return Boolean. The pointer will move forward on next()

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
0

iterator.hasNext() does not make the pointer forward, the iterator.next() does, so in the first loop, the iterator.next() return the first element of collection; and it returns the second element in second loop

Arafat Nalkhande
  • 11,078
  • 9
  • 39
  • 63
nnzhang
  • 21
  • 1