Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexOf in a string array

Is there anyway to get indexOf like you would get in a string.

output.add("1 2 3 4 5 6 7 8 9 10);  
String bigger[] = output.get(i).split(" ");
int biggerWher = bigger.indexOf("10");

I wrote this code but its returning an error and not compiling! Any advice ?

like image 301
chuck finley Avatar asked Jun 06 '11 08:06

chuck finley


2 Answers

Use this ...

output.add("1 2 3 4 5 6 7 8 9 10");  
String bigger[] = output.get(i).split(" ");
int biggerWher = Arrays.asList(bigger).indexOf("3");
like image 168
Saurabh Gokhale Avatar answered Oct 14 '22 08:10

Saurabh Gokhale


When the array is an array of objects, then:

Object[] array = ..
Arrays.asList(array).indexOf(someObj);

Another alternative is org.apache.commons.lang.ArrayUtils.indexOf(...) which also has overloads for arrays of primitive types, as well as a 3 argument version that takes a starting offset. (The Apache version should be more efficient because they don't entail creating a temporary List instance.)

like image 33
Stephen C Avatar answered Oct 14 '22 09:10

Stephen C