字符串数组中的indexOf

发布于 2024-11-13 06:56:12 字数 218 浏览 7 评论 0原文

无论如何,是否可以像在字符串中一样获取indexOf。

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

我编写了这段代码,但它返回错误并且无法编译!有什么建议吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

永言不败 2024-11-20 06:56:12

使用这个...

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

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");
负佳期 2024-11-20 06:56:12

当数组是对象数组时,则:

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

另一种选择是 org.apache.commons.lang.ArrayUtils.indexOf(...) 也具有原始类型数组的重载,以及采用起始偏移量的 3 参数版本。 (Apache 版本应该更高效,因为它们不需要创建临时 List 实例。)

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

陌伤ぢ 2024-11-20 06:56:12

数组没有 indexOf() 方法;但是,java.util.List 可以。因此,您可以将数组包装在列表中并使用 List 方法(add() 等除外):

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

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");
み零 2024-11-20 06:56:12

您可以使用 java.util.Arrays.binarySearch(array, item);
这将为您提供该项目的索引(如果有)...

但是请注意,在搜索之前需要对数组进行排序。

问候

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

风吹雨成花 2024-11-20 06:56:12

Java数组中没有直接的indexOf方法。

There is no direct indexOf method in Java arrays.

当爱已成负担 2024-11-20 06:56:12
output.add("1 2 3 4 5 6 7 8 9 10");

10点后你错过了"

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

you miss a " after 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文