简体   繁体   中英

Java Iterator going in infinite while loop

I am trying to iterate list with the iterator in while, for some reason its going in infinite.

here is the code

{
List<Person> Person1 = new ArrayList<Person>();

    Person p1 = new Person("Name1", "Virginia");

    Person1.add(new Person("Nae2", "Virginia"));

    printerlist(Person1);

    printerlist(p1);

}

private static void printerlist(List<Person> p) {

    /*
     * print the list
     */
    while (p.iterator().hasNext()) {
        System.out.println(p.iterator().next().getCity());
    }

}

Think about what this line is doing:

while (p.iterator().hasNext()) {

Each time you want to evaluate the condition, you're getting a new iterator object, that has never been touched before.

Since you've never consumed anything from the new iterator, hasNext() will always be true.

Likewise, on the next line:

    System.out.println(p.iterator().next().getCity());

You are getting another new iterator, so calling next() on it will always return the first item in the list.


In order to loop correctly, you need to reuse the same iterator for the entirety of the loop. You can do this either by explicitly creating and using a single iterator like so:

    Iterator<Person> itr = p.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next().getCity());
    }

or you can use something like an enhanced for-loop and let Java implicitly create and manage the iterator:

    while (Person person : p) {
        System.out.println(person.getCity());
    }

which is functionally equivalent to

    for (Iterator<Person> itr = p.iterator(); itr.hasNext();) {
        Person person = itr.next();
        System.out.println(person.getCity());
    }

p.iterator() creates a new Iterator for list p . So cursor always in begin of collection.

Try this:

Iterator<Person> it = p.iterator();
while (it.hasNext()) {
    System.out.println(it.next().getCity());
}

Every time you call p.iterator() you will get a new Iterator starting at the first element. Instead you should assign the returned Iterator to a temporary variable.

You do not need to use iterator() this will return object of type iteration( Iterator<T> ). It is use for

an interface (part of the Java Collections) that returns the type that was passed to it. The Iterator is used to traverse the list of elements, and remove an element if necessary.

We can captcher that iteration() from p and assign it in to Iterator with using generic <Person> .

You need to create Iterator<> for Person object type and then only need to use while(p.hasNext()) {

If you are using Java 1.5 or above you can use for each(enhanced for) loop to Iterate objects(list) type. Before 1.5 Iterator is used to iterate through lists. Notice Iterator still use.

If you want to know more about iterator() reffer this java doc and this or stackoverflow question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM