Hey everyone,
I'm programming (obviously) and have run into a challenge.
I'll cut right to the chase, if someone would tell me how to
compare a variable to each item in an array I would be very grateful.
Thanks for the help!
-WolfShield
Hey everyone,
I'm programming (obviously) and have run into a challenge.
I'll cut right to the chase, if someone would tell me how to
compare a variable to each item in an array I would be very grateful.
Thanks for the help!
-WolfShield
Looping over the array and comparing values would be the simplest solution. If it is an array of primitive values, use the comparison operator ==
. If it is an array of reference types, use the equals
method (or compareTo
if the reference type implements natural ordering).
Another simple way would be to make sure your array is sorted ( Arrays.sort
) and then use the Arrays.binarySearch
method.
http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html
http://www.java2s.com/Code/Java/Collections-Data-Structure/ArraySearchTest.htm
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.