Java - Endless loop with ListIterator .hasNext()
Asked Answered
F

8

5
ArrayList<String> list = new ArrayList<String>();
list.add("test");

while(list.listIterator().hasNext()) {
    System.out.println(list.listIterator().next());
}

This generates an endless loop of lines with "test". Why does this happen and how to fix it?

Fortran answered 18/5, 2016 at 11:34 Comment(2)
You are generating a new iterator at each loop step so as it is always brand new and your list contains an item, hasNext() always returns true.Desultory
you can alternatively do it in one line when you use a for loop: for (ListIterator<String> it = list.listIterator(); it.hasNext();) { /* looped code */ }, limits the scope of the iterator to just that loop.Ashla
M
15

The iterator created for hasNext is not the same that is for next.

ArrayList<String> list = new ArrayList<String>();
list.add("test");
Iterator listIterator = list.listIterator()
while(listIterator.hasNext()) {
        System.out.println(listIterator.next());
}
Millford answered 18/5, 2016 at 11:38 Comment(0)
N
3

Since you are creating a new iterator on each loop, the newly created iterator pointer will be positioned before the first element in the List. hasNext() method in that case will always return true and next() method will return the first element in the list.

Nixie answered 18/5, 2016 at 11:49 Comment(0)
N
1

Here:

System.out.println(list.listIterator().next());

you create an new iterator each time you try to print out the next element.

Just try the following:

ArrayList<String> list = new ArrayList<String>();

list.add("test");

Iterator<String> it = list.iterator();

while(it.hasNext()) {

    System.out.println(it.next());

}
Nepean answered 18/5, 2016 at 11:40 Comment(0)
P
1

Use this code,

List<String> list = new ArrayList<String>();
ListIterator<String> litr = null;
list.add("test");
litr=list.listIterator();
System.out.println("Elements in forward directiton");
while(litr.hasNext()){
   System.out.println(litr.next());
}
Printery answered 18/5, 2016 at 11:45 Comment(0)
S
0

You create new instance of iterator in every list.listIterator() call.

Create list iterator once, before loop starts to solve your problem.

Surtout answered 18/5, 2016 at 11:38 Comment(0)
I
0

Your calling listIterator everytime. Try assign to a variable and loop over that.

Involution answered 18/5, 2016 at 11:38 Comment(0)
W
0

list.listIterator() returns new Iterator. Below is source of AbstractList

  public ListIterator<E> listIterator() {
        return listIterator(0);
    }
  public ListIterator<E> listIterator(int location) {
        return new FullListIterator(location);
    }

You should make loop like this,

ArrayList<String> list = new ArrayList<String>();
list.add("test");
Iterator<String> iterator = list.listIterator();
while(iterator .hasNext()) {
        System.out.println(iterator.next());
        }
Warr answered 18/5, 2016 at 11:39 Comment(0)
F
0

Your problem is that you're creating new iterator at each list.listIterator() call. All the new instances will be at the beginning of the iterator. You can fix it if you use the same iterator:

    ArrayList<String> list = new ArrayList<String>();
    list.add("test");

    Iterator<String> iterator = list.listIterator();

    while(iterator.hasNext()) {
        System.out.println(iterator.next());
    }
Fail answered 18/5, 2016 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.