如何查找 int[] 的 ArrayList 是否包含 int[] [英] How can I find if my ArrayList of int[] contains an int[]

查看:20
本文介绍了如何查找 int[] 的 ArrayList 是否包含 int[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 int[]ArrayList.

I have an ArrayList containing int[].

假设 Arrays.equals(int[], (arrayList 中的一个值)) = true

为什么当我执行 arrayOfInts.contains(int[]) 时这是错误的?

why when I do arrayOfInts.contains(int[]) is this false?

我认为这是因为 (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(如果将来有人搜索这里是我作为解决方案制作的比较代码):

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[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆