Java的数组indexOf在哪里?
我一定错过了一些非常明显的东西,但我已经搜索遍了,但找不到这个方法。
I must be missing something very obvious, but I've searched all over and can't find this method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
有几种方法可以使用 来完成此操作
数组
实用程序类。如果数组未排序且不是基元数组:
如果数组是基元且未排序,则应使用提供的解决方案通过其他答案之一,例如Kerem Baydoğan的,安德鲁·麦金莱的或米沙克斯的。即使 theArray 是原始的(可能发出警告),上面的代码也会编译,但您仍然会得到完全不正确的结果。
如果数组已排序,您可以使用二分搜索来提高性能:
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:
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:
数组没有
indexOf()
方法。也许这个 Apache Commons Lang
ArrayUtils
方法就是您正在寻找的Array has no
indexOf()
method.Maybe this Apache Commons Lang
ArrayUtils
method is what you are looking for对于基元,如果您想避免装箱,Guava 有基元数组的帮助程序,例如 Ints.indexOf(int[] 数组,int 目标)
For primitives, if you want to avoid boxing, Guava has helpers for primitive arrays e.g. Ints.indexOf(int[] array, int target)
与 C# 不同,C# 中有 Array.IndexOf 方法,以及具有 indexOf 方法的 JavaScript,Java 的 API(
Array<特别是 /code> 和 Arrays 类)没有这样的方法。
此方法indexOf(连同其补充lastIndexOf)在java.util.List 接口。请注意,indexOf 和lastIndexOf 未重载,仅采用Object 作为参数。
如果您的数组已排序,那么您很幸运,因为 Arrays 类定义了一系列二元搜索方法的重载,该方法将以最佳性能找到您要查找的元素的索引(O(log n) 而不是 O(n),后者是您可以从 indexOf) 完成的顺序搜索中得到的结果。有四个注意事项:
数组必须按自然顺序或按您作为参数提供的比较器的顺序排序,或者至少所有“小于”键的元素必须位于该键之前数组中的元素以及“大于”键的所有元素都必须位于数组中该元素之后;
通常使用indexOf进行的测试以确定键是否在数组中(验证返回值是否不为-1)不适用于binarySearch。您需要验证返回值是否不小于零,因为返回的值将指示该键不存在,但指示该键存在时预期的索引;
如果你的数组包含多个与键相等的元素,那么你从binarySearch中得到的结果是未定义的;这与返回第一个出现的indexOf和返回最后一个出现的lastIndexOf不同。
如果布尔数组首先包含所有 false,然后包含所有 true,则它可能看起来已排序,但这不算数。没有重写接受布尔数组的 binarySearch 方法,如果您希望在检测第一个 true 出现在数组中的位置时获得 O(log n) 性能,例如使用数组布尔值以及常量 Boolean.FALSE 和 Boolean.TRUE。
如果您的数组未排序且不是原始类型,您可以通过调用 asList java.util.Arrays 方法。此方法将返回数组周围的 AbstractList 接口包装器。它涉及的开销最小,因为它不创建数组的副本。如前所述,此方法未重载,因此仅适用于引用类型的数组。
如果您的数组未排序,并且数组的类型是原始,那么您对 Java API 的使用就不那么幸运了。编写您自己的 for 循环或您自己的静态实用方法,这肯定比涉及对象实例化一些开销的 asList 方法具有性能优势。如果您担心编写一个遍历数组所有元素的强力 for 循环不是一个优雅的解决方案,请接受这正是 Java API 在您调用 indexOf 时所做的事情。您可以做这样的事情:
如果您想避免在这里编写自己的方法,请考虑使用像 Guava 这样的开发框架中的方法。在那里您可以找到 indexOf 和 lastIndexOf。
Unlike in C# where you have the Array.IndexOf method, and JavaScript where you have the indexOf method, Java's API (the
Array
andArrays
classes in particular) have no such method.This method indexOf (together with its complement lastIndexOf) is defined in the java.util.List interface. Note that indexOf and lastIndexOf are not overloaded and only take an Object as a parameter.
If your array is sorted, you are in luck because the Arrays class defines a series of overloads of the binarySearch method that will find the index of the element you are looking for with best possible performance (O(log n) instead of O(n), the latter being what you can expect from a sequential search done by indexOf). There are four considerations:
The array must be sorted either in natural order or in the order of a Comparator that you provide as an argument, or at the very least all elements that are "less than" the key must come before that element in the array and all elements that are "greater than" the key must come after that element in the array;
The test you normally do with indexOf to determine if a key is in the array (verify if the return value is not -1) does not hold with binarySearch. You need to verify that the return value is not less than zero since the value returned will indicate the key is not present but the index at which it would be expected if it did exist;
If your array contains multiple elements that are equal to the key, what you get from binarySearch is undefined; this is different from indexOf that will return the first occurrence and lastIndexOf that will return the last occurrence.
An array of booleans might appear to be sorted if it first contains all falses and then all trues, but this doesn't count. There is no override of the binarySearch method that accepts an array of booleans and you'll have to do something clever there if you want O(log n) performance when detecting where the first true appears in an array, for instance using an array of Booleans and the constants Boolean.FALSE and Boolean.TRUE.
If your array is not sorted and not primitive type, you can use List's indexOf and lastIndexOf methods by invoking the asList method of java.util.Arrays. This method will return an AbstractList interface wrapper around your array. It involves minimal overhead since it does not create a copy of the array. As mentioned, this method is not overloaded so this will only work on arrays of reference types.
If your array is not sorted and the type of the array is primitive, you are out of luck with the Java API. Write your own for loop, or your own static utility method, which will certainly have performance advantages over the asList approach that involves some overhead of an object instantiation. In case you're concerned that writing a brute force for loop that iterates over all of the elements of the array is not an elegant solution, accept that that is exactly what the Java API is doing when you call indexOf. You can make something like this:
If you want to avoid writing your own method here, consider using one from a development framework like Guava. There you can find an implementation of indexOf and lastIndexOf.
没有。使用
java.util.List
*,或者您可以编写自己的indexOf()
:*您可以使用
Arrays#asList()
There is none. Either use a
java.util.List
*, or you can write your ownindexOf()
:*you can make one from your array using
Arrays#asList()
Java
ArrayList
有一个indexOf
方法。 Java数组没有这样的方法。Java
ArrayList
has anindexOf
method. Java arrays have no such method.除了自己编码之外,我不记得数组上有“indexOf”...尽管您可能可以使用许多
java.util.Arrays#binarySearch(...)
方法之一(请参阅 数组 javadoc)你的数组包含原始类型I don't recall of a "indexOf" on arrays other than coding it for yourself... though you could probably use one of the many
java.util.Arrays#binarySearch(...)
methods (see the Arrays javadoc) if your array contains primitive typesList 接口有一个indexOf() 方法,您可以使用Array 的asList() 方法从数组中获取List。除此之外,Array本身没有这样的方法。它确实有一个用于排序数组的 binarySearch() 方法。
The List interface has an indexOf() method, and you can obtain a List from your array with Array's asList() method. Other than that, Array itself has no such method. It does have a binarySearch() method for sorted arrays.
数组本身没有这个方法。然而,列表可以:
indexOf
Arrays themselves do not have that method. A List, however, does:
indexOf
您可能会想到 java.util。 ArrayList,而不是数组。
You're probably thinking of the java.util.ArrayList, not the array.
java数组中没有直接的indexOf函数。
There is no direct indexOf function in java arrays.
Jeffrey Hantin 的答案很好,但它有一些限制,如果它这样做,或者这样做......
你可以编写自己的扩展方法,它总是按照您想要的方式工作。
这是你的扩展
Jeffrey Hantin's answer is good but it has some constraints, if its this do this or else to that...
You can write your own extension method and it always works the way you want.
And here is your extension
注意:您可以对int类型的数组使用此方法,也可以对其他类型稍加改动使用此算法
Note: you can use this method for arrays of type int, you can also use this algorithm for other types with minor changes