ArrayList <int []> contains the value

If I have an example ArrayList<int[]>and I want to check if it contains {2,4}, how would I do it?

exaple.contains({2,4}); //doesn't work

and

exaple.contains(2,4); //doesn't work either

what is wrong with the code?

+4
source share
4 answers

Arrays do not override the method Object.equals()(which is used ArrayList.contains()to compare objects). Thus, the array is only equal to itself. You will need to go through the list and compare each element with your array using Arrays.equals().

I suspect you should not have List<int[]>, but << 24>. The Coordinate class can then override equals()and hashCode(), and you can use

example.contains(new Coordinate(2, 4))

List<List<Integer>>, , , (.. , ), Coordinate.

+11

, :

int[] vals = {2, 4};

exaple.contains(vals);

true, contains equals. equals, false, .

0

. , , .

arraylist?

0

The method contains()checks if it ArrayListcontains a specific object; it does not apply to values ​​inside the object. In other words, if you create two arrays {2,4}, these will be two different objects, and the method contains()will differ between them.

What you need to do is either override the method contains()to see the contents of the arrays, not just the link, or you can completely abandon the method contains()and check it manually.

0
source

All Articles