两种方法
1。在一个数组中
[code]
int[] aa = new int[]{1,2,3,4,5};
int ii = 3;
if (Arrays.binarySearch(aa,ii) >= 0) {
}
[/code]
2。在一个list中
[code]
List aaList = new ArrayList();
aaList.add(new Integer(0));
aaList.add(new Integer(1));
aaList.add(new Integer(2));
Integer aaInt = new Integer(3);
if (aaList.contains(aaInt)) {
}[/code]
1。在一个数组中
[code]
int[] aa = new int[]{1,2,3,4,5};
int ii = 3;
if (Arrays.binarySearch(aa,ii) >= 0) {
}
[/code]
2。在一个list中
[code]
List aaList = new ArrayList();
aaList.add(new Integer(0));
aaList.add(new Integer(1));
aaList.add(new Integer(2));
Integer aaInt = new Integer(3);
if (aaList.contains(aaInt)) {
}[/code]
本文介绍了两种常用的元素查找方法:一种是在数组中使用 binarySearch 方法;另一种是在 List 中使用 contains 方法。这两种方法可以帮助开发者快速判断指定元素是否存在于集合中。

被折叠的 条评论
为什么被折叠?



