由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - How to check if an element is in an array?
相关主题
诡异问题求助问一个题
今天碰到一个面试题问一道关于Vector的题
如何遍历hashtable里边的每一项?array to list
一个好用的arrayToStringinterview question
请问一个有关选择数据结构的问题请问有没有generic的array
Simple question: delete element from collection on condition?问个primitive type的问题
让大家了解工业界Java/J2EE面试题的难度看了zhaoce073大水忍不住说2句
关于Vector的土问题问个数组问题
相关话题的讨论汇总
话题: array话题: element话题: do话题: java话题: check
进入Java版参与讨论
1 (共1页)
y********o
发帖数: 2565
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.
B*********h
发帖数: 800
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.

g*****g
发帖数: 34805
3
People usually do a for loop to check,
boolean found = false;
for(i=0; i if(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.

y********o
发帖数: 2565
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()
m******t
发帖数: 2416
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.

g*****g
发帖数: 34805
6
binary search will require a sorting in advance, which probably is not
what you want.

【在 m******t 的大作中提到】
: java.util.Arrays.binarySearch(int[], int)
o******t
发帖数: 1144
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.

y********o
发帖数: 2565
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

y********o
发帖数: 2565
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

B*********h
发帖数: 800
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?

r*****l
发帖数: 2859
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。

c*****t
发帖数: 1879
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
: 的办
: 我不

m******t
发帖数: 2416
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.

1 (共1页)
进入Java版参与讨论
相关主题
问个数组问题请问一个有关选择数据结构的问题
问两个语法问题Simple question: delete element from collection on condition?
Java里有没有象cell array一样的东西让大家了解工业界Java/J2EE面试题的难度
Generic type cast warning关于Vector的土问题
诡异问题求助问一个题
今天碰到一个面试题问一道关于Vector的题
如何遍历hashtable里边的每一项?array to list
一个好用的arrayToStringinterview question
相关话题的讨论汇总
话题: array话题: element话题: do话题: java话题: check