Using and declaring generic List

前端 未结 3 1640
离开以前
离开以前 2020-12-10 11:50

So I\'m working in Java and I want to declare a generic List.

So what I\'m doing so far is List list = new ArrayList();

But no

3条回答
  •  眼角桃花
    2020-12-10 12:14

    You should either have a generic class or a generic method like below:

    public class Test  {
        List list = new ArrayList();
        public Test(){
    
        }
        public void populate(T t){
            list.add(t);
        }
        public static  void main(String[] args) {
            new Test().populate("abc");
        }
    }
    

提交回复
热议问题