Dynamic initialization of ArrayList

后端 未结 3 1958
借酒劲吻你
借酒劲吻你 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:29

    This works:

    private void getModel(){
       ArrayList<?> arrayList = new ArrayList<Object>();
    }
    

    I mean, it is unclear what you are trying to do. Generics is purely compile-timem, to perform compile-time type checking. Therefore, if the type parameter is not known at compile time, it would be useless.

    0 讨论(0)
  • 2020-12-29 11:32

    Try using following

    public <T> List<T> getList(Class<T> requiredType) {
        return new ArrayList<T>();
    }
    
    public void useList() {
        List<Integer> ints = getList(Integer.class);
        List<String> lists = getList(String.class);
    }
    
    0 讨论(0)
  • 2020-12-29 11:41

    Try something like this

         private <T> void setModel(Class<T> type) {
          ArrayList<T> arrayList = new ArrayList<T>();
       }
    

    If you want to get the list back then

    private <T> ArrayList<T> getModel(Class<T> type) {
          ArrayList<T> arrayList = new ArrayList<T>();
          return arrayList;
       }
    

    EDIT

    A FULL EXAMPLE SHOWING HOW TO USE GENERIC TYPE FOR ARRAYLIST

    Tester class with main method and the generic Method

    public class Tester {
    
        private <T> ArrayList<T> getModels(Class<T> type) {
            ArrayList<T> arrayList = new ArrayList<T>();
            return arrayList;
        }
    
    
        public static void main(String[] args) {
            Data data = new Data(12, "test_12");
            Magic magic = new Magic(123, "test_123");
    
            Tester t = new Tester();
    
            ArrayList<Data> datas = (ArrayList<Data>) t.getModels(Data.class);
            datas.add(data);
            for(Data data2 : datas) {
                System.out.println(data2);
            }
    
            ArrayList<Magic> magics = (ArrayList<Magic>) t.getModels(Magic.class);
            magics.add(magic);
            for(Magic magic2 : magics) {
                System.out.println(magic2);
            }
    
        }
    
    }
    

    Another possibility to use the same things without parameter since we don't use it inside the method

    public class Tester {
    
        private <T> ArrayList<T> getModel() {
            ArrayList<T> arrayList = new ArrayList<T>();
            return arrayList;
        }
    
    
        public static void main(String[] args) {
            Data data = new Data(12, "test_12");
            Magic magic = new Magic(123, "test_123");
    
            Tester t = new Tester();
    
            ArrayList<Data> datas =  t.getModel();
            datas.add(data);
            for(Data data2 : datas) {
                System.out.println(data2);
            }
    
            ArrayList<Magic> magics = t.getModel();
            magics.add(magic);
            for(Magic magic2 : magics) {
                System.out.println(magic2);
            }
    
        }
    
    }
    

    Model class (Data)

    public class Data {
    
        private Integer id;
        private String name;
    
    
        public Data() {
        }
    
    
        public Data(Integer id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
    
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        @Override
        public String toString() {
            return "Data [" + (id != null ? "id=" + id + ", " : "") + (name != null ? "name=" + name : "") + "]";
        }
    
    }
    

    Model class (Magic)

    public class Magic {
    
        private Integer id;
        private String name;
    
    
        public Magic() {
        }
    
    
        public Magic(Integer id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
    
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        @Override
        public String toString() {
            return "Data [" + (id != null ? "id=" + id + ", " : "") + (name != null ? "name=" + name : "") + "]";
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题