0

I have an Arrays class which contains several utility methods. The two methods in question are:

@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;
}

I have another class which makes use of the char version like so:

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

When compiling with javac, it fails with an error like:

[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
Tunaki
  • 132,869
  • 46
  • 340
  • 423

0 Answers0