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


当前回答

没有。或者使用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()创建一个数组

其他回答

没有。或者使用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()创建一个数组

我不记得在数组上有一个“indexOf”,除了为自己编写它…如果数组包含基本类型,则可以使用java.util.Arrays#binarySearch(…)方法之一(参见Arrays javadoc)

不像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的实现。

Jeffrey Hantin的答案很好,但它有一些限制,如果它是这个做这个或那个…

你可以编写自己的扩展方法,它总是以你想要的方式工作。

Lists.indexOf(array, x -> item == x); // compare in the way you want

这是您的分机号码

public final class Lists {
    private Lists() {
    }

    public static <T> int indexOf(T[] array, Predicate<T> predicate) {
        for (int i = 0; i < array.length; i++) {
            if (predicate.test(array[i])) return i;
        }
        return -1;
    }

    public static <T> int indexOf(List<T> list, Predicate<T> predicate) {
        for (int i = 0; i < list.size(); i++) {
            if (predicate.test(list.get(i))) return i;
        }
        return -1;
    }

    public interface Predicate<T> {
        boolean test(T t);
    }
}

使用Arrays实用程序类有几种方法可以实现这一点。

如果数组没有排序,也不是基元数组:

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

如果数组是原语且没有排序,则应该使用由其他答案之一提供的解决方案,如Kerem baydo土耳其的,Andrew McKinlay的或Mishax的。即使theArray是基本的(可能会发出警告),上面的代码也会被编译,但您仍然会得到完全错误的结果。

如果数组是排序的,你可以使用二进制搜索性能:

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