Dynamic initialization of ArrayList<anyClassObject>

99封情书 提交于 2019-12-18 11:51:15

问题


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

ArrayList<?> arrayList = new ArrayList<MyClass.class>();

But I want to do something similar to this no matter which class object I pass, i.e

private void getModel(Class responseType){

   //Something similar, because this does not work..                                                       
   ArrayList<?> arrayList = new ArrayList<responseType>();
}

Any Help would be greatly appreciated.


回答1:


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 : "") + "]";
    }

}



回答2:


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.




回答3:


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);
}


来源:https://stackoverflow.com/questions/15697775/dynamic-initialization-of-arraylistanyclassobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!