zoukankan      html  css  js  c++  java
  • java ArrayList

    ArrayList的remove(Object obj)方法使用equals判断list中的元素,如下

    public boolean remove(Object o) {  
        if (o == null) {  
                for (int index = 0; index < size; index++)  
            if (elementData[index] == null) {  
                fastRemove(index);  
                return true;  
            }  
        } else {  
            for (int index = 0; index < size; index++)  
            if (o.equals(elementData[index])) {  
                fastRemove(index);  
                return true;  
            }  
            }  
        return false;  
        }  
    

    对equals()方法重写的对象(如String、Integer),删除的只是值为obj的元素

    String str = "Sun Wukong";

    list.add(str);

    list.remove(new String("Sun Wukong")); 或 list.remove("Sun Wukong");都可以删除list中的元素str

    Element ele = new Element("Hello");

    list.add(ele);

    list.remove(new Element("Hello")); 则不能删去list中的元素ele

    必须list.remove(ele);

  • 相关阅读:
    Roadblocks(poj 3255)
    最小集合(51nod 1616)
    绿色通道(codevs 3342)
    解的个数(codevs 1213)
    多米诺(codevs 3052)
    abcd
    dwarf tower
    第K 小数
    noip2016复习
    文化之旅(洛谷 1078)
  • 原文地址:https://www.cnblogs.com/deltadeblog/p/7861951.html
Copyright © 2011-2022 走看看