回答
如果要检查看,如果某个值存储在ArrayList
可以使用方法,这将返回true
如果对象是在列表中,false
否则。
ArrayList<Integer> intList = new ArrayList<>();
intList.add(5);
intList.add(7);
intList.add(3);
intList.add(-2);
intList.contains(-1); //returns false
intList.contains(3); //returns true
很好的答案!谢谢。 – 2017-03-24 05:03:42
您可能想要使用ArrayList.contains()
来检查元素是否在ArrayList
中。
不,你没有。但这里是我最好的猜测:
List<Integer> values = Arrays.asList{ 1, 2, 4, -5, 44 };
int userValue = 44;
boolean containsUserValue = values.contains(userValue);
if (!containsUserValue) {
values.add(userValue);
}
为什么不直接写'if(!values.contains(userValue))'? – user3932000 2018-01-13 21:25:29
可以做到这一点。没有增加太多价值,特别是在问题首次被问到六年后。找到一个更好的方法来提升你的代表。 – duffymo 2018-01-13 22:28:43
我不打算“提高我的代表”,无论这应该是什么意思。我只是想帮助人们在将来阅读这个问题,因为人们*会阅读旧的问题。 – user3932000 2018-01-13 23:01:02
如果我理解你的问题,你想检查一个数组列表是否已经包含整数值。如果是这样,你可以使用ArrayList.contains()。
示例代码浏览:
ArrayList list = new ArrayList();
int x = 4, y = 7;
int z = x;
list.add(x);
//List contains the value 4, which is the value stored in z
//Program will output "List contains 4"
if(list.contains(z))
{
System.out.printf("List contains %d\n", z);
}
else
{
System.out.printf("List does not contain %d\n", z);
}
//List contains the value 7, which is the value stored in y
//Program will output "List does not contain 7"
if(list.contains(y))
{
System.out.printf("List contains %d\n", y);
}
else
{
System.out.printf("List does not contain %d\n", y);
}
- 1. ArrayList数据需要在JTable中表示
- 2. 我需要找到一个合适的数据结构
- 3. 我需要找到一个数据结构
- 4. Python:TypeError:需要一个整数
- 5. 我需要总结一个整数的个别数字在python
- 6. 发送只有一个整数到一个函数,需要两个整数
- 7. 我需要有一个数据库
- 8. Java - 我需要一个数据库吗?
- 9. 为什么我得到TypeError:需要一个整数?
- 10. 我如何找到一个整数中的9个数
- 11. 找到一个整数
- 12. Matplotilb-需要找到源数据从一个类属性
- 13. 找到一个整数到另一个整数的百分比
- 14. 需要将整个postgreSQL数据库加载到内存中
- 15. 如何将一个有序整数ArrayList中的元素添加到另一个整数有序ArrayList中?
- 16. 类型错误:一个整数需要
- 17. datetime.date:TypeError:需要一个整数。为什么?
- 18. web2py,TypeError:需要一个整数
- 19. Python os.popen需要一个整数?
- 20. Celery SchedulingError:需要一个整数
- 21. 的Socket程序需要一个整数
- 22. 我需要修改一个表结构,并需要找到数据库中的特定列
- 23. 需要在java数组中找到重复的整数和它们的数量
- 24. 找到一个ArrayList中
- 25. 我需要的第一个日志在数据库中的Java
- 26. 需要获得乘数,将带来一个分的分数到一个整数
- 27. 我可以从数据文件中找到mysql版本,需要恢复数据
- 28. 我需要得到一个数组中最大的数字recursevely
- 29. 类“数据库”未找到(在这里需要一些帮助)
- 30. 每次我是否需要在循环中新建一个ArrayList?
你想分享什么,你到目前为止试过吗? – dasblinkenlight 2012-02-18 16:25:57
在问这样一个问题之前,先看看ArrayList API。注意到ArrayList类的定义中有一个contains()方法,这很简单。 – Juvanis 2012-02-18 16:34:07