In Python you can do something like
if 7 in list
return True
Is there anything in java like this? To go "if x in array" without having to do a for loop or several lines of code?
Thanks
In Python you can do something like
if 7 in list
return True
Is there anything in java like this? To go "if x in array" without having to do a for loop or several lines of code?
Thanks
Java arrays don't have such properties, but you can either use a collection (preferable a Set, because the lookup methods are the most efficient) or wrap your array with Arrays.asList()
return Arrays.asList(arr).contains(7)
You can use
Arrays.asList(yourArray) - convert array to list
and then
.contains(7) - find value at list
Some other solutions: http://javarevisited.blogspot.cz/2012/11/4-ways-to-search-object-in-java-array-example.html
Convert your array to list using Arrays#asList() and
There is contains()
method in List
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object)
You need to call Collection
contains
method to check for existense:
list.contains(7)