Friday, November 18, 2011

java: how to easily check if an element is present in an array

I'm spoiled by Python - it's so easy to do some things like check the presence of an item in a list:

mylist = ['a', 'b', 'c']
if 'a' in mylist:
     print "It's there!"

So what's the equivalent of this in Java?

if( Arrays.asList("a","b","c").contains("a") )
    system.out.println( "It's there!" );

Thanks to this stackoverflow discussion for the answer.

No comments: