1

I know it is a bit confusing, but i basically wanna check if an arrayList of integer Arrays contains an array or not? I have added a code for more sense.

I was trying the following code. But .contains method doesnt seem to work when checking for an array. An alternative method will be very helpful.

int[] data = {1, 2};
int[] data2 = {3, 4};
int[] dataCheck = {1, 2};

ArrayList<int[]> megaData = new ArrayList<int[]>();
megaData.add(data);
megaData.add(data2);

if (megaData.contains(dataCheck)) {
    System.out.println("A copy has been found");
} else {
    System.out.println("No copy found");
}
Shahid
  • 2,288
  • 1
  • 14
  • 24
HumparDumpar
  • 47
  • 1
  • 5
  • I think the problem is that the ArrayList is checking for equality in the contains Method: http://stackoverflow.com/questions/2642589/how-does-a-arraylists-contains-method-evaluate-objects – Georg Leber Sep 18 '16 at 14:42
  • *"How do i use `contains` method here?"* **You don't.** `contains()` uses the `equals()` method and Java arrays don't implement `equals()`. – Andreas Sep 18 '16 at 14:42
  • Thanks, but can you tell me a way to accomplish this task then? – HumparDumpar Sep 18 '16 at 14:42
  • 1
    Write a loop, and call [`Arrays.equals()`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#equals-int:A-int:A-). – Andreas Sep 18 '16 at 14:44
  • Ah, sorry i am very new to java. Can you help me by writing the code? Solution for the above problem would help heaps. Thanks – HumparDumpar Sep 18 '16 at 14:48

2 Answers2

2

contains does not work in this case because the equals-method of an array only checks if the array-objects are the same, not if they have the same elements (cp. this post). And contains uses equals.

My recommendation is to use only Listss instead of arrays. Lists have an equals-method that compares the lists' elements. The first lines of the code have to be changed for this:

List<Integer> data  = Arrays.asList(1,2);
List<Integer> data2 = Arrays.asList(3,4);
List<Integer> dataCheck = Arrays.asList(1,2);
List<List<Integer>> megaData= new ArrayList<List<Integer>>();

Arrays.asList is an easy way to create a list with fixed elements. Lists are usually declared with only the interface List and not the implementing class like ArrayList. This makes it easy to use another List-implementation at a later point of time.

Community
  • 1
  • 1
mm759
  • 1,404
  • 1
  • 9
  • 7
0

That could be:

boolean contains = false;
for (int[] element : megaData) { // iterating over the list
    if (Arrays.equals(element, dataCheck)) {
        contains = true;
        break;
    }
}

if (contains) {
    System.out.println("A copy has been found");
} else {
    System.out.println("No copy found");
}

Alternatively, you could write a method for this:

public boolean containsArray(ArrayList<int[]> listOfArrays, int[] array) {
    for (int[] element : listOfArrays) {
        if (Arrays.equals(element, array)) {
            return true;
        }
    }
    return false;
} 
Shahid
  • 2,288
  • 1
  • 14
  • 24