Why does this java iterator loop print also the first element?

Nicola Amadio
import java.util.*;
public class IteratorDemo {

   public static void main(String args[]) {
      // Create an array list
      ArrayList al = new ArrayList();

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");

      // Use iterator to display contents of al
      System.out.print("Original contents of al: ");
      Iterator itr = al.iterator();

      while(itr.hasNext()) {
         Object element = itr.next();
         System.out.print(element + " ");
      }
      System.out.println();
    }
  }

I don't get it. If al.iterator() returns the first element of al, why if I put itr.next()(which should be the second I'm guessing) in element and then print element does it print the first element and not the second?

I'm new to java. Making a parallel with c/c++, is Iterator like a pointer and Iterator.next() like the dereferenced pointer? Can someone explain to me what's actually happening at a low level when I do what's in the code?

Ousmane D.

Calling the iterator() returns an iterator over the elements in the list, not the first element. in fact if this returned the first element, then the condition while(itr.hasNext()) wouldnt make any sense.

It's only when you call next() that the next element in the iteration is returned.

So, if you want to exclude the first element then you'll need to do:

 if(itr.hasNext()) itr.next();

before the loop.

Also, as an aside I'd recommend you use parameterized types rather than raw types i.e. your ArrayList should be declared ArrayList<String> al = new ArrayList<>(); and your iterator as Iterator<String> itr = al.iterator();.

Given that all you want to do in this case is to print the element to the console there is no need to use an Iterator at all as it's simpler to do:

 al.forEach(element -> System.out.print(element + " "));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does `zip` eat one more element from the first iterator?

Why does this PHP assignment in a loop always print the first value?

Why does my for loop only print the first result?

Why does this Iterator infinitely loop?

why does my for loop only work on the first element?

why boost::python iterator skips first element?

Why is iterator of container invalidated when this container is also an element of a container?

Why does my print execute after the second loop even if I use print first?

Why does for each loop in java does not access the last element?

Why does an iterator of `ToString` items requires them to be `Display` also?

Why does a for loop not require a mutable iterator?

Why does the iterator in a JavaScript for loop not need a semicolon?

Why does a for loop not require the iterator to be mutable?

Why does my java guessing game not work after the first loop

Why does the Java.Util.Stack not pop last element in the loop?

Why does printing the first element of a pointer to pointer to a char print the contents of a string?

Why does my doubly linked list iterator print null?

why i need to add double quote to print first and last element of char array in java

Why is my for loop only grabbing first element?

Why does setTimeout only print once in a loop?

Why does the for loop print out a string in this situation

Why does this nested for-loop print this?

Why does this for loop print 6 and 10?

java- reset list iterator to first element of the list

Why does the compiler not complain that an iterator moved to a for loop is immutable?

Why does my iterator foreach loop never enter/execute?

Why does findIndex not return the first found element?

Why does this loop break at the first iteration?

Why does the second for loop overwrite the first in Python?