3

So when I run this program it shows that the ArrayList "test" doesn't contain the array [5,6] inside the variable "position".When I checked the output, it is clearly in there and i see that "test" does contain that element.


Output:

[5, 6] [5, 6] false

Code:

package arraylisttest;

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListTest {
    public static void main(String[] args) {
        int[] position = { 5, 6 };
        ArrayList<int[]> test = new ArrayList<>();

        test.add(new int[] {50, 2});
        test.add(new int[] {0, 7});
        test.add(new int[] {5, 6});
        test.add(new int[] {2, 1});

        System.out.println(Arrays.toString(position));
        System.out.println(Arrays.toString(test.get(2)));
        System.out.println(test.contains(position));
    }
}
2
  • What is your question? Commented May 25, 2017 at 1:49
  • ArryList#contains(obejct) will check whethet object is in the list.In your case, position is different the object created by 'new int[] {50, 2}' Commented May 25, 2017 at 2:19

3 Answers 3

3

I believe that List.contains() will use the equals() method to determine if the list contains a given object (q.v. the source code for ArrayList#contains()). It will not compare the two points in each 2D array to see if they be the same. So even though the point {5, 6} logically appears in the list, it is a different object than position which you are using for the comparison, and hence the comparison fails.

Note that the following code would have behaved as you expected:

int[] position = { 5, 6 };
ArrayList<int[]> test = new ArrayList<>();
test.add(new int[] {50, 2});
test.add(new int[] {0, 7});
test.add(position);
test.add(new int[] {2, 1});

System.out.println(Arrays.toString(position));
System.out.println(Arrays.toString(test.get(2)));

System.out.println(test.contains(position));
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, contains(), in this case, would look to see if the array references are the same using ==.
Haha, yea I was assuming that Arrays.equals() would have been called during contains() since it is being treated as an object. Meh, I guess it's not implemented that way.
3

The problem here is that arrays don't override Object#equals method hence the output you're receiving; rather you can create ArrayList of ArrayLists instead e.g. the code below will output true.

ArrayList<Integer> position = new ArrayList<>(Arrays.asList(5, 6));
ArrayList<ArrayList<Integer>> test = new ArrayList<>();
ArrayList<Integer> y = new ArrayList<>(Arrays.asList(5,6));
test.add(y);
System.out.println(test.contains(position));

another option would be to leave your current solution as it is, but use a stream to perform a comparison against the other arrays within the list:

System.out.println(test.stream().anyMatch(e -> Arrays.equals(e,position)));

Comments

0

Arrays can only be compared with Arrays.equals().

You should use ArrayList instead:

ArrayList<Integer> position = new ArrayList<Integer>(Arrays.asList(5, 6));

ArrayList<ArrayList<Integer>> test = new ArrayList<>();
test.add(new ArrayList<>(Arrays.asList(50, 2)))
test.add(new ArrayList<>(Arrays.asList(0, 7)));
test.add(new ArrayList<>(Arrays.asList(5, 6)));
test.add(new ArrayList<>(Arrays.asList(2, 1)));


System.out.println(test.contains(position));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.