I've initialized an ArrayList holding int arrays as such:
ArrayList<int[]> holder = new ArrayList<int[]>();
If I add an int[] like this:
int[] first = {1, 2, 3};
holder.add(first);
If I do this check, I want the function to return true, but it returns false right now
int[] second = {2, 1, 3};
if(holder.contains(second)) return true;
else return false
CodePudding user response:
You can't use ArrayList#Contains
to judge since int[]
don't have a special equals
. You can iterate list and then compare:
private static boolean contains(ArrayList<int[]> holder, int[] arr) {
for (int[] item : holder) {
if (equals(item, arr)) {
return true;
}
}
return false;
}
private static boolean equals(int[] arr1, int[] arr2) {
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}