Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using contains on an ArrayList with integer arrays

I have an ArrayList<int[]>, and I add an array to it.

ArrayList<int[]> j = new ArrayList<int[]>();
int[] w = {1,2};
j.add(w);

Suppose I want to know if j contains an array that has {1,2} in it without using w, since I will be calling it from another class. So, I create a new array with {1,2} in it...

int[] t = {1,2};
return j.contains(t);

...but this would return false even though w was added to the list, and w contains the exact same array as t.

Is there a way to use contains such that I can just check to see if one of the elements of the ArrayList has the array value {1,2}?

like image 445
kamikaze_pilot Avatar asked Jan 31 '11 08:01

kamikaze_pilot


People also ask

Can we use contains in ArrayList?

contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.

How do you check if an ArrayList contains an array?

contains() method in Java is used to check whether or not a list contains a specific element. To check if an element is in an array, we first need to convert the array into an ArrayList using the asList() method and then apply the same contains() method to it​.

Can an ArrayList contain arrays?

ArrayList of arrays can be created just like any other objects using ArrayList constructor. In 2D arrays, it might happen that most of the part in the array is empty. For optimizing the space complexity, Arraylist of arrays can be used.

Can ArrayList contain int?

ArrayList or any other collection cannot store primitive data types such as int.


2 Answers

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

You probably want an ArrayList of ArrayLists.

ArrayList<ArrayList<Integer>> j = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> w = new ArrayList<Integer>();
w.add(1); w.add(2);
j.add(w);
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(1); t.add(2);
return j.contains(t); // should return true.
like image 132
Clement P Avatar answered Sep 24 '22 13:09

Clement P


The problem here is that arrays don't override Object.equals(Object), So the comparison between two list entries happens with the default equals() implementation

// from Object.class
public boolean equals(Object obj) {
return (this == obj);
}

So you have to iterate over the list and check all entries using Arrays.equals(int[], int[]). Here's a Helper method that does this:

public static boolean isInList(
    final List<int[]> list, final int[] candidate){

    for(final int[] item : list){
        if(Arrays.equals(item, candidate)){
            return true;
        }
    }
    return false;
}

Update: Ever since Java 8, this has got a lot simpler:

public static boolean isInList(
        final List<int[]> list, final int[] candidate) {

    return list.stream().anyMatch(a -> Arrays.equals(a, candidate));
            //  ^-- or you may want to use .parallelStream() here instead
}
like image 29
Sean Patrick Floyd Avatar answered Sep 25 '22 13:09

Sean Patrick Floyd