How to remove duplicate in list while element is an array?

Why falsedoes Java code return ?

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

And when the items in the list are arrays, how to remove the duplicate, then?

Thanks for your reply.

+4
source share
1 answer

Arrays do not override the implementation Object equals, therefore, since it list.contains()uses equalsto determine if an element appears in List, it will return true only if you are looking for the exact array object added to List. Therefore, it list.contains(arr[1])will return true, but list.contains(temp)it will not, because tempthey arr[1]are different objects (although they contain the same elements).

List<List<Integer> List<int[]>, list.contains() , ( List Object equals).

+4

Source: https://habr.com/ru/post/1671520/


All Articles