编译

时间:2016-03-02 19:20:39

标签: java java-8

我有一个Arrays类,其中包含多个实用程序方法。有问题的两种方法是:

@SafeVarargs
public static <T> int indexOf(T needle, T ... haystack) {
    for (int i = 0; i < haystack.length; i++) {
        if (needle == null?haystack[i] == null:needle.equals(haystack[i])) return i;
    }
    return -1;
}

public static int indexOf(char needle, char ... haystack) {
    for (int i = 0; i < haystack.length; i++) {
        if (needle == haystack[i]) return i;
    }
    return -1;
}

@SafeVarargs
public static <T> boolean contains(T needle, T ... haystack) {
    return indexOf(needle, haystack) != -1;
}

public static boolean contains(char needle, char ... haystack) {
    return indexOf(needle, haystack) != -1;
}

我有另一个使用char版本的类:

char type = getType();
Arrays.contains(type, 'B', 'C', 'D', 'E');

使用javac进行编译时,会失败并显示如下错误:

[ERROR] /path/to/MyClass.java:[63,30] reference to contains is ambiguous
  both method <T>contains(T,T...) in Arrays and method contains(char,char...) in Arrays match

0 个答案:

没有答案