Question: I have an array list of 2d arrays and the `contains()` method does not seem to work. 

```
if (visitedBFS.contains(multi2)) 
{    
    frontier.add(multi2);    
    visitedBFS.add(multi2); 
}

```

The 2D array, which is already in the Array List should not be added to a queue, however it is added to frontier again and again.

Is there another method? 

Do you have any suggestions?

Thank you.

Answer: 
The problem is that arrays don't override the `equals` method, so they have the behaviour of the default implementation, i.e:

```
150    public boolean equals(Object obj) {
151        return (this == obj);
152    }

```

This is what contains call to see if the list contains the element you search.

If you take this:

```
List<String[][]> l = new ArrayList<>();
l.add(new String[][]{{"one"},{"two"}});

String[][] arr = {{"one"},{"two"}};
System.out.println(l.contains(arr)); //false

```

It outputs `false` because the two arrays don't point to the same memory location.

---

You can create an helper method, using [`Arrays.deepEquals`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29):

```
static boolean contains(List<String[][]> l,  String[][] arr){
    for(String[][] array : l){
        if(Arrays.deepEquals(array, arr)){
            return true;
        }
    }
    return false;
}

...

System.out.println(contains(l, arr)); //true

```

Another possibility would be to use a class that hold your array and then override `equals`.

```
class MyArrayHolder {
    private String[][] arr;

    public MyArrayHolder(String[][] arr){
        this.arr = arr;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.deepHashCode(arr);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MyArrayHolder other = (MyArrayHolder) obj;
        if (!Arrays.deepEquals(arr, other.arr))
            return false;
        return true;
    }   
}

```

And then using a `List<MyArrayHolder>`

```
public static void main(String[] args) throws Exception{
    List<MyArrayHolder> l = new ArrayList<>();
    l.add(new MyArrayHolder(new String[][]{{"one"},{"two"}}));
    String[][] arr = {{"one"},{"two"}};
    System.out.println(l.contains(new MyArrayHolder(arr))); //true
}

```
Let me **guess**, what you want:

```
static int[][] mainTab = new int[][] { { 1, 0, 0, 5 }, { 0, 1, 0, 4 },
        { 1, 2, 1, 7 } };

public static void main(String[] args) {
    System.out.println(contains(new int[] { 1, 0, 0, 5 }));
    System.out.println(contains(new int[] { 7, 7, 7, 5 }));
    System.out.println(contains(new int[] { 1, 2, 1, 77 }));
}

public static boolean contains(int[] tab) {
    for (int i = 0; i < 3; i++) {
        boolean isOkRow = true;
        for (int j = 0; j < 4; j++) {
            if (mainTab[i][j] != tab[j]) {
                isOkRow = false;
            }
        }
        if (isOkRow) {
            return true;
        }
    }
    return false;
}

```

with output:

```
true
false
false

```