Understanding Java Iterator

故事扮演 提交于 2021-01-20 07:16:46

问题


If I run the following code, it will print out 3 times duplicate, but when I remove the if statement inside the while loop (just to see how many times it will iterate) it starts an infinite loop.

How does actually this hasNext() method working? I thought that will iterate only 5 times as I have 5 items in the list.

    public class ExerciseOne {
    public static void main(String []args){
        String []colors = {"MAGENTA","RED","WHITE","BLUE","CYAN"};
        List<String> list = new ArrayList<String>();
        for(String color : colors)
            list.add(color);
        String[] removeColors = {"RED","WHITE","BLUE"};
        List<String> removeList = new ArrayList<String>();
        for(String color : removeColors)
            removeList.add(color);

        removeColors(list,removeList);
        System.out.printf("%n%nArrayList after calling removeColors:%n");
        for(String color : list)
        {
            System.out.printf("%s ",color);
        }
    }

    private static void removeColors(Collection<String> collection1, Collection<String> collection2)
    {
        Iterator<String> iterator = collection1.iterator();

            while(iterator.hasNext()){
                if(collection2.contains(iterator.next()))
                    System.out.println("duplicate");
            }
    }

}

回答1:


It is pretty simple, actually

while(iterator.hasNext()){
    if(collection2.contains(iterator.next()))
       System.out.println("duplicate");
}

Imagine that the iterator is a pointer to an element of your list.

When you call next(), you're moving this pointer one step ahead.

If you don't move the pointer, hasNext() will always be true because you're still in the beginning of the list.

So you have to call the iterator's next() until there isn't any remaining element in the list.




回答2:


If you remove the if statement, then it will go for an infinite loop since your iterator.next() is in the if condition. Actually iterator.next() is the api that moves the pointer, not the hasNext(). hasNext() just checks if there is any element in the collection. Since removal of the if statement is also removing the hasNext api, the pointer to the collection is not moving and hasNext is always returning true.

If you take out the iterator.next() from the if condition and move it above the if condition, then the loop will iterate for 5 times even after you remove the if statement.

Iterator<String> iterator = collection1.iterator();
while(iterator.hasNext()){
      String currentColor = iterator.next();
      if(collection2.contains(currentColor)){
         System.out.println("duplicate");
      }
}



回答3:


The question why Iterator is important/introduced is simple:
consider following example:
List<String> list = new ArrayList<String>();
list.add("Anurag");
list.add("Soni");
list.add("MMM");
list.add("GKP");

for(string s : list){
  if(s.equals(" Anurag")
  s.remove();
  System.out.println(s);
}

This will throw an exception-`Concurrent Modification exception` as you are trying to alter the structure of the data structure List before the iteration is completed.

so you may use Iterator for the same purpose .
Iterator iterator = List.iterator();

while(iterator.hasNext()){
String current = iterator.next();
if(current=="Anurag"){
  iterator.remove();
}else{
System.out.println(current);
}

}
OUTPUT:  Soni
         MMM
         GKP


来源:https://stackoverflow.com/questions/34775888/understanding-java-iterator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!