zoukankan      html  css  js  c++  java
  • list for循环中删除元素

    Iterator.remove() is safe, you can use it like this:
    
    List<String> list = new ArrayList<>();
    
    // This is a clever way to create the iterator and call iterator.hasNext() like
    // you would do in a while-loop. It would be the same as doing:
    //     Iterator<String> iterator = list.iterator();
    //     while (iterator.hasNext()) {
    for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
        String string = iterator.next();
        if (string.isEmpty()) {
            // Remove the current element from the iterator and the list.
            iterator.remove();
        }
    }
    Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress
  • 相关阅读:
    153. Find Minimum in Rotated Sorted Array
    228. Summary Ranges
    665. Non-decreasing Array
    661. Image Smoother
    643. Maximum Average Subarray I
    4.7作业
    面向对象编程
    常用模块3
    3.31作业
    常用模块2
  • 原文地址:https://www.cnblogs.com/programerlrc/p/5620024.html
Copyright © 2011-2022 走看看