Answers for "java return new instance of generic type"

0

java return new instance of generic type

public static <E> void append(List<E> list, Class<E> cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}
//You can invoke the append method as follows:
List<String> ls = new ArrayList<>();
append(ls, String.class);
Posted by: Guest on May-16-2020

Code answers related to "java return new instance of generic type"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language