-4

Possible Duplicate:
In Java, how can I test if an Array contains a certain value?

I want to check for a string S whether it is present in string array or not. Is there any direct method to do so ?

EDIT

If the answer excludes the use of List then that would be better.

Community
  • 1
  • 1
program-o-steve
  • 2,630
  • 15
  • 48
  • 67
  • If by *"direct"* you mean a one-liner, then no. `ArrayList` and some other collections have a `contains()` method that absracts away the details. – Andrew Thompson May 25 '12 at 09:53
  • @Andrew Thompson i could use `indexOf()` for this. but i have to check all the rows in column number 0 in the 2-D String array against a string. Is there any way i can use `indexOf` with it ? – program-o-steve May 25 '12 at 09:59

2 Answers2

2

There are a couple of ways to accomplish this using the Arrays utility class.

If the array is not sorted:

java.util.Arrays.asList(theArray).indexOf(o)

If the array is sorted, you can make use of a binary search for performance:

java.util.Arrays.binarySearch(theArray, o)

See here Where is Java's Array indexOf?

Community
  • 1
  • 1
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

You can convert it to List and check: Arrays.asList(arrayVariable).contains(S)

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118