【问题标题】:How to print and remove elements in iterator simultaneously?如何同时打印和删除迭代器中的元素?
【发布时间】:2013-07-26 15:02:26
【问题描述】:

我正在浏览 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]   

【问题讨论】:

    标签: java iterator while-loop


    【解决方案1】:

    如果我同时使用 while 循环,迭代器不会从列表中删除元素,为什么?

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

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

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

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

    【讨论】:

      【解决方案2】:

      你正在做:

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

      所以在第一个 while 循环之后,您的迭代器将完成。

      【讨论】:

        猜你喜欢
        • 2019-01-19
        • 1970-01-01
        • 2013-03-23
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 2012-03-28
        相关资源
        最近更新 更多