我一定是错过了一些非常明显的东西,但我已经搜索了所有,不能找到这个方法。


当前回答

不像c#中有数组。IndexOf方法,JavaScript中有IndexOf方法,Java的API(尤其是Array和Arrays类)没有这样的方法。

这个方法indexOf(连同它的补充lastIndexOf)是在java.util.List接口中定义的。注意,indexOf和lastIndexOf不是重载的,它们只接受一个Object作为参数。

如果您的数组已经排序,那么您很幸运,因为Arrays类定义了binarySearch方法的一系列重载,这些重载将以最好的性能(O(log n)而不是O(n),后者是您可以从indexOf执行的顺序搜索中获得的结果)找到您正在寻找的元素的索引。有四个考虑因素:

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.

如果你的数组没有排序,也不是基本类型,你可以通过调用java.util.Arrays的asList方法来使用List的indexOf和lastIndexOf方法。该方法将返回围绕数组的AbstractList接口包装器。它涉及的开销最小,因为它不创建数组的副本。如上所述,此方法没有重载,因此只对引用类型的数组有效。

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:

public static int indexOfIntArray(int[] array, int key) {
    int returnvalue = -1;
    for (int i = 0; i < array.length; ++i) {
        if (key == array[i]) {
            returnvalue = i;
            break;
        }
    }
    return returnvalue;
}

如果您不想在这里编写自己的方法,可以考虑使用来自像Guava这样的开发框架的方法。在那里你可以找到indexOf和lastIndexOf的实现。

其他回答

Java数组列表有一个indexOf方法。Java数组没有这样的方法。

您可能会想到java.util。数组列表,而不是数组。

不像c#中有数组。IndexOf方法,JavaScript中有IndexOf方法,Java的API(尤其是Array和Arrays类)没有这样的方法。

这个方法indexOf(连同它的补充lastIndexOf)是在java.util.List接口中定义的。注意,indexOf和lastIndexOf不是重载的,它们只接受一个Object作为参数。

如果您的数组已经排序,那么您很幸运,因为Arrays类定义了binarySearch方法的一系列重载,这些重载将以最好的性能(O(log n)而不是O(n),后者是您可以从indexOf执行的顺序搜索中获得的结果)找到您正在寻找的元素的索引。有四个考虑因素:

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.

如果你的数组没有排序,也不是基本类型,你可以通过调用java.util.Arrays的asList方法来使用List的indexOf和lastIndexOf方法。该方法将返回围绕数组的AbstractList接口包装器。它涉及的开销最小,因为它不创建数组的副本。如上所述,此方法没有重载,因此只对引用类型的数组有效。

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:

public static int indexOfIntArray(int[] array, int key) {
    int returnvalue = -1;
    for (int i = 0; i < array.length; ++i) {
        if (key == array[i]) {
            returnvalue = i;
            break;
        }
    }
    return returnvalue;
}

如果您不想在这里编写自己的方法,可以考虑使用来自像Guava这样的开发框架的方法。在那里你可以找到indexOf和lastIndexOf的实现。

List接口有一个indexOf()方法,您可以使用array的asList()方法从数组中获取List。除此之外,Array本身没有这样的方法。它确实有一个用于排序数组的binarySearch()方法。

没有。或者使用java.util。List*,或者你可以自己写indexOf():

public static <T> int indexOf(T needle, T[] haystack)
{
    for (int i=0; i<haystack.length; i++)
    {
        if (haystack[i] != null && haystack[i].equals(needle)
            || needle == null && haystack[i] == null) return i;
    }

    return -1;
}

*你可以使用数组#asList()创建一个数组