Generics in static methods

后端 未结 2 850
Happy的楠姐
Happy的楠姐 2021-01-24 10:42

I need to add a method in a utility class with some static methods that parses things from a JSON string and returns an array of things.

Problem is that there are variou

2条回答
  •  情书的邮戳
    2021-01-24 11:00

    From memory this is a similar problem to what you have when using Arrays.asList()

    With this you do

    Arrays.(new MyObject(), new MyObject());
    

    or in your specific case where you are passing an array eg

    Arrays.asList(new Integer[]{new Integer(1)});
    

    The signature in the JDK for this method is

    public static  List asList(T... a){...}
    

    which is similar to your I think

提交回复
热议问题