Two Wrong Writings for Iterator Traversal Collection [java]

Keywords: Java Back-end

Two Writing Errors for Iterator Traversal Sets

Error Mode One:

eg:

package iterator;

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorDemo5 {
    public static void main(String[] args) {
        Collection c1 = new ArrayList();
        c1.add(1234);
        c1.add("abc");
        c1.add(7890);
        c1.add(false);
        c1.add(true);
        Iterator iterator = c1.iterator();
        /*
        This is the wrong way to write when our while loop determines if the return value of the iterator.next() method is null.
        When we output, we jump to the output, start with the second element, and there must be a NoSuchElementException exception, because at this point we decide
        while Whether or not the loop jumps is conditioned by the next() method, at which point we must end up with a pointer (our iterator) pointing to null, because only our
        The pointer does not exit the while loop until it points to null, which means that a sufficient and necessary condition for us to exit the while loop is the presence of NoSuchElementException
         */
        while(iterator.next() != null){
            System.out.println(iterator.next());
        }
    }
}
  • We only use the next() method in this way, we don't use the hasNext() method, but we call the next() method twice here, where one of our next() methods is used as a condition to enter the while() loop and the other next() method is written in the while loop to traverse collection elements
    • At this point our output set elements start with the second element in the set and are output one at a time
    • And will certainly throw the NoSuchElementException exception, because our while loop is determined by next() = null; Judging, so at this point we have to exit the while loop if and only if the pointer (iterator) points to null, which means that we exit the while loop if and only if there is a NoSuchElementException exception

Error Mode 2:

eg:

package iterator;

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorDemo6 {
    public static void main(String[] args) {
        Collection c1 = new ArrayList();
        c1.add(1234);
        c1.add("abc");
        c1.add(false);
        c1.add(5678);
        /* 
        At this time, we create a new Iterator object each time we enter the while loop, then the new iterator we create will point to the first element before, at this time, our while loop will become a dead loop
        This is when we create a new Iterator object each time in our while loop, and each time we output it, it's the first element in our collection
        So the final implementation is the first element in the output set, and the output that doesn't end at this time is a dead loop
         */
        while(c1.iterator().hasNext()){
            System.out.println(c1.iterator().next());
        }
    }
}

  • A new Iterator object is created each time we enter the while loop, and then the new iterator we create points to the first element, and then our while loop becomes an endless loop
  • The while loop also creates a new Iterator object each time, and each time we output it, it is the first element in our collection
  • The final implementation is the first element in the output set, and the output that does not end at this time is a dead loop

Summary: When iterators are used to traverse collection objects, it is important to note that the best way we can traverse is in our normal format
That is, the format of the while loop in our example below:

package iterator;

import java.util.Collection;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Iterator;

public class IteratorDemo4 {
    public static void main(String[] args) {
        Collection c1 = new ArrayList();
        c1.add(1234);
        c1.add("abc");
        c1.add(false);
        c1.add(new Dog3());
        c1.add(1.5);
        /* 
        Here we create an iterator object
         */
        Iterator iterator =  c1.iterator();
        /*
        Here we use mode three to traverse our collection elements, which is the way most of our actual programming uses to traverse them.
        And this way we can completely avoid NoSuchElementException
         */
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
class Dog3{
    private String name;
    private int age;
    public Dog3(){
        
    }
    public Dog3(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog3 dog3 = (Dog3) o;
        return age == dog3.age &&
                Objects.equals(name, dog3.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Dog3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Posted by Rayne on Thu, 02 Dec 2021 10:33:21 -0800