How can I cast a list using generics in Java?

前端 未结 5 1544
刺人心
刺人心 2021-02-02 09:20

Please consider the following snippet:

public interface MyInterface {

    public int getId();
}

public class MyPojo implements MyInterface {

    private int i         


        
5条回答
  •  盖世英雄少女心
    2021-02-02 10:18

    You should be doing:

    public ArrayList getMyInterfaces() {   
       ArrayList myPojos = new ArrayList(0);    
       myPojos.add(new MyPojo(0));    
       myPojos.add(new MyPojo(1));    
       return myPojos;
    }
    

提交回复
热议问题