Java 的 Array indexOf 在哪里? [英] Where is Java's Array indexOf?

查看:48
本文介绍了Java 的 Array indexOf 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定遗漏了一些非常明显的东西,但我已经搜索了很多,找不到这个方法.

I must be missing something very obvious, but I've searched all over and can't find this method.

推荐答案

有几种方法可以使用 Arrays 实用程序类.

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

如果数组未排序并且不是原始数组:

If the array is not sorted and is not an array of primitives:

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

如果数组原语且未排序,则应使用其他答案之一提供的解决方案,例如 Kerem Baydoğan'sAndrew McKinlay's米沙克斯.即使 theArray 是原始代码(可能会发出警告),上面的代码也会编译,但您仍然会得到完全不正确的结果.

If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan's, Andrew McKinlay's or Mishax's. The above code will compile even if theArray is primitive (possibly emitting a warning) but you'll get totally incorrect results nonetheless.

如果数组已排序,则可以使用二进制搜索来提高性能:

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

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

这篇关于Java 的 Array indexOf 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆