zoukankan      html  css  js  c++  java
  • 遍历List、Map删除元素

    遍历List删除元素

    方法一:

    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add(null);
    list.add("5");
    
    for(int i=0; i<list.size(); i++){
        list.remove(i);
        i--;
    }
    System.out.println(list.size());


    方法二:

    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add(null);
    list.add("5");
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
        String tmp = (String)iterator.next();
        iterator.remove();
    }
    System.out.println(list.size());

     

    遍历Map删除元素

    方法一: 

    Map<String,String> map = new HashMap<>();
    map.put("1","aaa");
    map.put("2","bbb");
    map.put("3","ccc");
    map.put("4","ddd");
    map.put("5","eee");
    
    Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        it.remove();
    }
    System.out.println(map.size());

     
    方法二:

    Map<String,String> map = new HashMap<>();
    map.put("1","aaa");
    map.put("2","bbb");
    map.put("3","ccc");
    map.put("4","ddd");
    map.put("5","eee");
    
    for (Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, String> entry = it.next();
        it.remove();
    }
    System.out.println(map.size());
  • 相关阅读:
    Attributes in C#
    asp.net C# 时间格式大全
    UVA 10518 How Many Calls?
    UVA 10303 How Many Trees?
    UVA 991 Safe Salutations
    UVA 10862 Connect the Cable Wires
    UVA 10417 Gift Exchanging
    UVA 10229 Modular Fibonacci
    UVA 10079 Pizza Cutting
    UVA 10334 Ray Through Glasses
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/deleteListMap.html
Copyright © 2011-2022 走看看