Specifying a generic type in java from Class object

后端 未结 7 1503
野的像风
野的像风 2020-12-11 16:19

Why this is wrong:

    Class type = Integer.class;
    ArrayList = new ArrayList<>();

?

I

相关标签:
7条回答
  • 2020-12-11 16:56

    You don't even need to pass in an argument:

    public <T extends Number> ArrayList<T> createAList () {
        return new ArrayList<T>();
    }
    

    Though you may need to explicitly specify the type parameter when calling:

    ArrayList<Integer> intList = this.<Integer>createAList();
    
    0 讨论(0)
提交回复
热议问题