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

标签 java iterator while-loop

我正在浏览 Java 中的集合,并遇到了一些迭代器示例。我给出下面的代码

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]

为什么?

如果我使用两个 while 循环,迭代器不会从列表中删除元素,为什么?你能帮我吗。

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);

        }
        }

上面的代码给出了输出

[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?

因为当第一个循环完成时,迭代器的 hasNext 为 false。因此代码根本不会进入第二个循环。

要第二次循环,您必须获得一个新的迭代器。

在支持它的集合中,您可以使用remove在一个循环中打印和删除。迭代器的方法。

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

关于java - 如何同时打印和删除迭代器中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17884828/

相关文章:

java - IntelliJ 插件开发 : insert new code after all fields within a class

java - 创建空流并向其中添加元素

使用 STL 算法与容器(char * 除外)进行 C++ 二进制文件 I/O

java - 将映射从 java 迭代到 jsp

java - 传递给调用方法的命令行参数

javascript - JavaScript 中的 While 循环和递增

java - 如何为无效返回方法定义 AnswersWithDelay

c++ - 使用迭代器访问存储在 vector 中的类对象的方法。如何?

java - 如何在 Java 中输入多个数据单元并使用计数器

java - 无法解析日期 ["2016-08-29 11:08:37.645007"] : Invalid time zone indicator ' '