public class FindNumber {
static String findNumber(List<Integer> arr, int k) {
String res = "YES";
//Unable to identify problem with this part of the code
for (int i = 0; i < arr.size(); i++) {
if (k == arr.get(i))
res = "YES";
else
res = "NO";
}
return res;
}
}
即使整数存在于列表中,上面的代码也会返回 NO 作为答案。
最佳答案
您可以使用arr.contains()
来获取Integer
是否在列表中的 boolean 值。然后您可以将此值转换为 YES
或 NO
(如果您确实需要它):
String yesNo = arr.contains(k) ? "YES" : "NO";
关于java - 在java中的arraylist中搜索整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57091128/