How do I restrict accepting only one type in my generic method?
- by kunjaan
I have a generic function foo, which accepts any type and prints them out.
public static <T> T foo(T... arg) {
List<T> foo = Arrays.asList(arg);
for (T t : foo) {
System.out.println(t);
}
return null;
}
How do I make sure that the arguments received are of only 1 type. For example, {1,'a',3} should be invalid. It should either be all numbers or all characters.