Java中List的contains方法为什么不能实现二维数组中的去重?
迷茫
迷茫 2017-04-18 10:48:06
0
1
905

请教为什么以下代码会返回false?

        List list = new ArrayList();
        int[][] arr = {{-1, -1, 2}, {-1, 0, 1}};
        list.add(arr[0]);
        list.add(arr[1]);
        int[] temp = {-1, 0, 1};
        return list.contains(temp);

当list中的元素是数组时,如何实现去重?谢谢回答

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
洪涛

equals is used internally

    public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }
        public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
    

You can rewrite it to compare all elements

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template