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.
|