Dynamic initialization of ArrayList

后端 未结 3 1957
借酒劲吻你
借酒劲吻你 2020-12-29 11:04

Normally if we want to initialize a generic non-primitive ArrayList we do this

ArrayList arrayList = new ArrayList();
<         


        
3条回答
  •  醉梦人生
    2020-12-29 11:32

    Try using following

    public  List getList(Class requiredType) {
        return new ArrayList();
    }
    
    public void useList() {
        List ints = getList(Integer.class);
        List lists = getList(String.class);
    }
    

提交回复
热议问题