K
K
Kirill2020-07-05 20:33:35
Java
Kirill, 2020-07-05 20:33:35

Why doesn't the listIterator interface fire a second time?

ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("First");
        arrayList.add("Second");
        arrayList.add("Third");
        arrayList.add("Fourth");
        arrayList.add("Fifth");
        arrayList.add("Sixth");

        ListIterator<String> listIterator = arrayList.listIterator();

        while (listIterator.hasNext()){
            System.out.println(listIterator.next());
        }
        
        System.out.println("-------");
        
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex());
            listIterator.next();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2020-07-05
@MakRaimov

Because iterators remember their state, and you can't enter this water a second time - you need to take the iterator again!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question