开发者

indexOf in a string array

开发者 https://www.devze.com 2023-03-10 10:21 出处:网络
Is there anyway to get indexOf like you would get in 开发者_JAVA技巧a string. output.add(\"1 2 3 4 5 6 7 8 9 10);

Is there anyway to get indexOf like you would get in 开发者_JAVA技巧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 ?


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");


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.)


Arrays do not have an indexOf() method; however, java.util.List does. So you can wrap your array in a list and use the List methods (except for add() and the like):

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


You can use java.util.Arrays.binarySearch(array, item); That will give you an index of the item, if any...

Please note, however, that the array needs to be sorted before searching.

Regards


There is no direct indexOf method in Java arrays.


output.add("1 2 3 4 5 6 7 8 9 10");

you miss a " after 10.

0

精彩评论

暂无评论...
验证码 换一张
取 消