Specifying a generic type in java from Class object

后端 未结 7 1541
野的像风
野的像风 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  ArrayList createAList () {
        return new ArrayList();
    }
    

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

    ArrayList intList = this.createAList();
    

提交回复
热议问题