在Java中检查整个ArrayList
我有一个简单的想法,但我不知道如何正确地实施它。。。 交易如下:
假设有一个名为myList的数组列表,大小为5,然后是 一个称为testNumber的整数。myList和/或testNumber中的值不相关
我需要做的是将testNumber变量与myList的每个整数进行比较
如果testNumber不等于它们中的任何一个,则执行一个操作(即System.out.println("hi");
)
我该怎么办
你可以在下面搜索框中键入要查询的问题!
我有一个简单的想法,但我不知道如何正确地实施它。。。 交易如下:
假设有一个名为myList的数组列表,大小为5,然后是 一个称为testNumber的整数。myList和/或testNumber中的值不相关
我需要做的是将testNumber变量与myList的每个整数进行比较
如果testNumber不等于它们中的任何一个,则执行一个操作(即System.out.println("hi");
)
我该怎么办
# 1 楼答案
ArrayList有一个名为contains()的方法,适用于此任务:
您可以按如下方式使用它:
# 2 楼答案
你就是这么做的
严格来说,这可能不会将每个元素与
testNumber
进行比较。因此,如果你真的对比较每个元素的“手动”方式感兴趣,以下是方法: