How do I restrict accepting only one type in my generic method?

Posted by kunjaan on Stack Overflow See other posts from Stack Overflow or by kunjaan
Published on 2010-05-13T04:44:41Z Indexed on 2010/05/13 4:54 UTC
Read the original article Hit count: 131

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about generic