我有一个ArrayList包含int[].
假设Arrays.equals(int[], (a value in the arrayList)) = true
为什么我做arrayOfInts.contains(int[])这是假的?
我认为是(int[] == (a value in the arrayList)) = false的相同原因,但我应该得到此错误吗?
I assume it is because of the same reason that (int[] == (a value in the arrayList)) = false but should I be getting this error?
我可以以某种方式纠正它吗?
Can I correct it somehow?
TL; DR:我可以将Arraylist.contains()用于int []
TL;DR: Can I use Arraylist.contains() for int[]
答案(如果将来有人在这里搜索,这是我作为解决方案编写的比较代码):
ANSWER (if anyone searches this in the future here's the comparison code I made as a solution):
private boolean arraylistContains(int[] comparison, ArrayList<int[]> arrayListToCompare){ for(int[] values: arrayListToCompare){ if (Arrays.equals(comparison, values)){ return true; } } return false; }推荐答案
数组列表仅存储对数组的引用,而不存储其内容,因此,如果列表包含确切的引用,则contains()仅能说到您输入的数组.它不评估数组的内容.
A list of arrays stores only the references to the arrays, not their content, therefore contains() is only able to say, if the list contains the exact reference to the array you input. It doesn't evaluate the contents of the array.
如果需要检查列表是否包含具有特定值的数组,则必须遍历列表并使用 Arrays.equals(arr1,arr2).
If you need to check, if the list contains an array with specific values, you will have to iterate over the list and use Arrays.equals(arr1, arr2).
更多推荐
如何确定我的int []的ArrayList是否包含int []
发布评论