Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic initialization of ArrayList<anyClassObject>

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.

like image 993
Ahmad Ali Nasir Avatar asked Mar 29 '13 05:03

Ahmad Ali Nasir


People also ask

Does ArrayList have dynamic size?

ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Array lists are created with an initial size.

How do you initialize an ArrayList array?

1.1. To initialize an arraylist in single line statement, get all elements in form of array using Arrays. asList method and pass the array argument to ArrayList constructor. ArrayList<String> names = new ArrayList<String>( Arrays. asList( "alex" , "brian" , "charles" ) );


2 Answers

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

}
like image 137
Festus Tamakloe Avatar answered Sep 22 '22 08:09

Festus Tamakloe


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.

like image 45
newacct Avatar answered Sep 22 '22 08:09

newacct