简体   繁体   English

如何同时打印和删除迭代器中的元素?

[英]How to print and remove elements in iterator simultaneously?

I was going through collections in Java and came across some iterator examples. 我正在浏览Java中的集合,并遇到了一些迭代器示例。 I am giving the code below 我在下面的代码

public class iteratorexample {

    public static void main(String[] args) {

        String removeE="New York";
        List<String> thelist = new ArrayList<String>();

        thelist.add("Dallas");
        thelist.add("Richmond");
        thelist.add("Atlanta");
        thelist.add("New York");
        thelist.add("Birmingham");
        System.out.print(thelist);
        System.out.println();
        Iterator<String> itr= thelist.iterator();
        /*while(itr.hasNext()){

            System.out.print(" " +itr.next()+" ");
        } */
        **while(itr.hasNext()){
            String remo=(String)itr.next();
            if(remo.equals(removeE)){
                itr.remove();

            }

        }**

        System.out.println();
        System.out.println("After removing: ");
        System.out.println(thelist);

        }
        }

Above Code give output 
[Dallas, Richmond, Atlanta, New York, Birmingham]

After removing: 
[Dallas, Richmond, Atlanta, Birmingham]

Why? 为什么?

If I use both the while loops the iterator does not remove the element from the list, why? 如果我同时使用while循环,则迭代器不会从列表中删除元素,为什么? Can you help me out. 你能帮我吗。

public static void main(String[] args) {

        String removeE="New York";
        List<String> thelist = new ArrayList<String>();

        thelist.add("Dallas");
        thelist.add("Richmond");
        thelist.add("Atlanta");
        thelist.add("New York");
        thelist.add("Birmingham");
        System.out.print(thelist);
        System.out.println();
        Iterator<String> itr= thelist.iterator();
        while(itr.hasNext()){

            System.out.print(" " +itr.next()+" ");
        } 
        while(itr.hasNext()){
            String remo=(String)itr.next();
            if(remo.equals(removeE)){
                itr.remove();

            }

        }

        System.out.println();
        System.out.println("After removing: ");
        System.out.println(thelist);

        }
        }

Above code gives output 上面的代码给出了输出

[Dallas, Richmond, Atlanta, New York, Birmingham]
 Dallas  Richmond  Atlanta  New York  Birmingham 
After removing: 
[Dallas, Richmond, Atlanta, New York, Birmingham]   

If I use both the while loops the iterator does not remove the element from the list, why? 如果我同时使用while循环,则迭代器不会从列表中删除元素,为什么?

Because when the first loop is complete, the iterator's hasNext is false. 因为当第一个循环完成时,迭代器的hasNext为false。 So the code never enters the second loop at all. 因此,代码根本不会进入第二个循环。

To loop a second time, you'd have to get a new iterator. 要第二次循环,您必须获得一个新的迭代器。

In collections that support it, you could both print and remove in one loop by using the remove method of the iterator. 在支持它的集合中,您可以使用迭代器的remove方法在一个循环中进行打印和删除。

while(itr.hasNext()){
    String remo=(String)itr.next();
    System.out.print(" " +remo+" ");
    if(remo.equals(removeE)){
        itr.remove();
    }
}

You are doing: 您正在执行:

while(itr.hasNext()){
   System.out.print(" " +itr.next()+" ");
} 
while(itr.hasNext()){

so after first while loop your iterator will be completed. 因此,在第一次while循环之后,您的迭代器将完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 java 中同时添加和删除 collections 中的元素? - how to add and remove elements in collections simultaneously in java? 如何使用 Java 中的迭代器从通用数组列表中打印元素 - How to print elements from generic array list, using iterator in Java 如何在特定条件下通过迭代器从 Set 中删除元素? - How to remove elements from Set by iterator with specific conditions? 如何在不使用迭代器进行迭代的同时从集合中删除元素 - How to remove elements from collection while iterating without iterator 如何分隔迭代器的元素? - How to separate the elements of an iterator? 如何从列表中删除“ $”字符并打印列表中所有元素的总和 - How to remove '$' character from the list and print sum of all the elements in the list 如果HashSet,ArrayList返回的迭代器是快速失败的,我们如何使用迭代器remove()从Collection中删除元素 - If Iterators returned by HashSet, ArrayList are fail-fast, how can we use iterator remove() to remove elements from Collection 从迭代器中删除数组中的元素的最佳方法是什么? - What is the best way to remove elements in an array from an Iterator? Java LinkedList迭代器:删除当前元素及其后的所有元素 - Java LinkedList iterator: Remove current element and all elements after it 在for循环内使用Iterator从ArrayList中删除元素 - Using an Iterator inside a for loop to remove elements from an ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM