开发者

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 t开发者_运维问答hough 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}?


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.


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
}


You need to iterate through the list and manually check whether an array matches your criteria.

public static boolean containsSubArray(List<int[]> j, int[] sub) {
   for ( int[] arr : j ) {
      if (arr contains elements of sub) {
         return true;
      }
   }
   return false;
}

If you want an exact match, you can make use of Arrays.equals(). I don't think there's a library function to do a contains all on an array though, so you would have to write that yourself if that's what you wanted.


"contains" contract checks for equality. So in your case what is failing is equality of int[]. Since Array does not override the equals method from Object you will need a workaround to check for containment.

If you need to check for containment within the Array then you are left with no choice but to iterate through the ArrayList and do the comparison yourself.


from java api:

public boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

since int[] is a primitive, im pretty sure no .equals method exists so its my guess it would always return false.

I would recommend a different way of storing the data? maybe with a key of some sort?


Two java array arrays are equal iff they have the same object reference. Content doesn't matter.

You're looking for a way to check if they have an equal content. This could help:

 Arrays.equals(new int[]{1,2}, new int[]{1,2});   // evaluates to true
 Arrays.equals(new int[]{1,2}, new int[]{2,1});   // evaluates to false (!)

If order shouldn't affect equality, then you will have to implement a static equals method by yourself.


First they are not the same Object reference, so they are not equal. equals() will return false. For your condition, you will need to implement a method to compare them yourself.


If you have one array and want to compare that all elements of array are present in list:

Long[] array1 = {1111L, 1112L};
Long[] array2 = {1111L, 1114L};
List<Long> list = new ArrayList<>();
list.add(1111L);
list.add(1112L);
list.add(1113L);

Arrays.asList(array1).stream().allMatch(val -> list.contains(val)); //return true
Arrays.asList(array2).stream().allMatch(val -> list.contains(val)); //return false
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜