记录ArrayList的一个坑

  在写算法时遇到了这么一个需求:

用ArrayList去保存ArrayList,然后当作返回值返回。

  里面那层ArrayList保存的是int数据,外面那层保存的是ArrayList。涉及到ArrayList的add和addAll还有remove操作。有这么一段代码:

//错误
if(root.left!=null){
            ArrayList<Integer> templ = new ArrayList();
            result.add(templ);
            templ.addAll(rootList);
            result.remove(rootList);
            Traverse(root.left,templ);
        }

//正确
if(root.left!=null){
            ArrayList<Integer> templ = new ArrayList();
            result.remove(rootList);
            result.add(templ);
            templ.addAll(rootList);
            Traverse(root.left,templ);
        }

  其中result就是保存ArrayList的那个List。templ和rootList都是保存int数据的ArrayList。rootList和templ经验证是两个不同的对象,以上两种写法得到的结果却不同。
  带着疑惑翻了一下源码才找到答案:

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

  以上是ArrayList的remove方法。第一个想到的就是可能equals被重写了,但是ArrayList的equals并没有重写,无意间打开AbstractList才真相大白:

public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof List))
            return false;

        ListIterator<E> e1 = listIterator();
        ListIterator<?> e2 = ((List<?>) o).listIterator();
        while (e1.hasNext() && e2.hasNext()) {
            E o1 = e1.next();
            Object o2 = e2.next();
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }
        return !(e1.hasNext() || e2.hasNext());
    }

  简单说只要都是List然后每个元素相等就代表它们是equals的。(还是觉得==靠谱)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值