Redian新闻
>
How to check if an element is in an array?
avatar
How to check if an element is in an array?# Java - 爪哇娇娃
y*o
1
In ArrayList, we can use contains(Object o) which returns a boolean value.
How about an array of primitive integers? In Python, we can do
if x in myList:
print "I got you!"
Any easy way to do the same in Java? I have always been wondering about
this.
Thanks.
avatar
B*h
2
==

【在 y********o 的大作中提到】
: In ArrayList, we can use contains(Object o) which returns a boolean value.
: How about an array of primitive integers? In Python, we can do
: if x in myList:
: print "I got you!"
: Any easy way to do the same in Java? I have always been wondering about
: this.
: Thanks.

avatar
g*g
3
People usually do a for loop to check,
boolean found = false;
for(i=0; iif(o.equals(array[i]) {
found = true;
break;
}
}
if you are really lazy, try Arrays.asList(array).contains()
For trivial array, shouldn't be much different in performance.

【在 y********o 的大作中提到】
: In ArrayList, we can use contains(Object o) which returns a boolean value.
: How about an array of primitive integers? In Python, we can do
: if x in myList:
: print "I got you!"
: Any easy way to do the same in Java? I have always been wondering about
: this.
: Thanks.

avatar
y*o
4
Thanks a lot! Yes, I also do for loop, but I had thought that there must be
some smarter way of doing that. The one you offered looks good.
发信人: goodbug (好虫), 信区: Java
标 题: Re: How to check if an element is in an array?
发信站: BBS 未名空间站 (Thu Nov 23 11:38:48 2006), 转信
People usually do a for loop to check, if you are really lazy,
try Arrays.asList(array).contains()
avatar
m*t
5
java.util.Arrays.binarySearch(int[], int)

【在 y********o 的大作中提到】
: In ArrayList, we can use contains(Object o) which returns a boolean value.
: How about an array of primitive integers? In Python, we can do
: if x in myList:
: print "I got you!"
: Any easy way to do the same in Java? I have always been wondering about
: this.
: Thanks.

avatar
g*g
6
binary search will require a sorting in advance, which probably is not
what you want.

【在 m******t 的大作中提到】
: java.util.Arrays.binarySearch(int[], int)
avatar
o*t
7
even the for loop has more lines, the performance is the same. ArrayList.
contains just do the same thing. In Java 5, we can use foreach

【在 y********o 的大作中提到】
: In ArrayList, we can use contains(Object o) which returns a boolean value.
: How about an array of primitive integers? In Python, we can do
: if x in myList:
: print "I got you!"
: Any easy way to do the same in Java? I have always been wondering about
: this.
: Thanks.

avatar
y*o
8
其实我在考虑这个问题是因为我想生成比方说100个random integer, 要个个不同。所以
想,生成一个放入array, 往后生成的,先看看array里面是否已经存在,是新鲜的才放
进去。
下午又想起来,可能可以用HashSet,将生成的random integer拿Integer包了放入Hash
Set里面去,这样就省了比较这一步了。
如果java的Collection classes可以接收primitive type就好了。像上面用HashSet的办
法,到头来还要打开包转换一下方能使用,不是很麻烦吗?
当然,如果根本就有极其简单的办法得到100个unique random integers, 请赐教。我不
熟java。

【在 o******t 的大作中提到】
: even the for loop has more lines, the performance is the same. ArrayList.
: contains just do the same thing. In Java 5, we can use foreach

avatar
y*o
9
I just tried the new "for" syntax for a hashset like this:
for (String mystrElement: myHashSet)
It worked. Hmm, much better than getting the iterator and then do hasNext().
Is this "for" syntax supposed to work for all Collection classes?

【在 o******t 的大作中提到】
: even the for loop has more lines, the performance is the same. ArrayList.
: contains just do the same thing. In Java 5, we can use foreach

avatar
B*h
10
arrays too.

().

【在 y********o 的大作中提到】
: I just tried the new "for" syntax for a hashset like this:
: for (String mystrElement: myHashSet)
: It worked. Hmm, much better than getting the iterator and then do hasNext().
: Is this "for" syntax supposed to work for all Collection classes?

avatar
r*l
11
You should definitely use Set instead of Array to do the job. Java 1.5 has
auto boxing and un-boxing so you don't have to do the conversion.

所以
Hash
的办
我不

【在 y********o 的大作中提到】
: 其实我在考虑这个问题是因为我想生成比方说100个random integer, 要个个不同。所以
: 想,生成一个放入array, 往后生成的,先看看array里面是否已经存在,是新鲜的才放
: 进去。
: 下午又想起来,可能可以用HashSet,将生成的random integer拿Integer包了放入Hash
: Set里面去,这样就省了比较这一步了。
: 如果java的Collection classes可以接收primitive type就好了。像上面用HashSet的办
: 法,到头来还要打开包转换一下方能使用,不是很麻烦吗?
: 当然,如果根本就有极其简单的办法得到100个unique random integers, 请赐教。我不
: 熟java。

avatar
c*t
12
autoboxing should be avoided.

【在 r*****l 的大作中提到】
: You should definitely use Set instead of Array to do the job. Java 1.5 has
: auto boxing and un-boxing so you don't have to do the conversion.
:
: 所以
: Hash
: 的办
: 我不

avatar
m*t
13
Right. For some reason I took for granted the OP was talking about a sorted
array.

【在 g*****g 的大作中提到】
: binary search will require a sorting in advance, which probably is not
: what you want.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。