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

原文地址:https://www.cnblogs.com/deltadeblog/p/7861951.html