Create a parametrized List from a Class variable

tehAnswer

I want to create a list from a Class variable.

...
Class clazz = someObject.getClass(); 
List<clazz> myList = new ArrayList<clazz>(); // this line is a compilation error
...

How I can do it?

EDIT: I think you are getting it wrong. For example, if someObject is a Car, then myList type should be List<Car>. I want to treat my clazz variable as a type, in some way.

fachammer

EDIT:

As Boris the Spider pointed out in the comments it actually is possible by using a generic method, but with a slight modification:

Instead of using the normal Class object, use the generic version Class<? extends [Type of someObject]>.

Example:

public static void main(String[] args){
    Test t = new Test();
    Class<? extends Test> testClass = t.getClass();
    List<? extends Test> list = createListOfType(testClass);
}

private static <T> List<T> createListOfType(Class<T> type){
    return new ArrayList<T>();
}

Of course you can also just go with

public static void main(String[] args){
    Test t = new Test();
    List<? extends Test> list = createListOfType(t);
}

private static <T> List<T> createListOfType(T element){
    return new ArrayList<T>();
}

OLD POST:

You can't.

You can't, because Java needs to know the generic type you want to use for the ArrayList at compile time. It's possible to use Object as type though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a parametrized List from a Class variable

From Dev

How to create new object of Generic Type T from a parametrized List<T>

From Dev

create and use class from variable

From Dev

create and use class from variable

From Dev

Can parameters from a parametrized class be used in external function definitions?

From Dev

Create list of class variables from list of objects

From Dev

Inheritance with parametrized class

From Dev

How to create a variable from CSS class attribute?

From Dev

How to create custom class from variable? (not Object)

From Dev

Create object from class stored in variable

From Dev

Python class variable referenced from within list

From Dev

Python class variable referenced from within list

From Dev

How to create case class from List[String]?

From Dev

Create a CSV of class members from a list<T>

From Dev

Parametrized object cannot be resolved to variable

From Dev

Having parametrized class in swagger annotations

From Dev

How set a variable in Mysql from Visual Basic .net using parametrized Value from a Texbox

From Dev

Create an dyanamic list from a List(of Class) with where condition

From Dev

create a string list from a generic class and and apply TrueForAll on that list

From Dev

Create an instance of a list of Class (1) that inherits from another list of class(2) with a list from the other (2)

From Dev

Swift 2.0: Parametrized classes don't call proper == function if it inherits from class that is Equatable

From Dev

Why is it possible to get back an object of "incorrect-type" from the parametrized List in Java?

From Dev

Create binned variable from results of class interval determination

From Dev

Can't create a variable in class that inherits from Atom api

From Dev

Create a class based on a variable

From Dev

Accessing Linked List private variable from outside class

From Dev

how to pass two variable values from a java util list class

From Dev

Shell: how to create a list variable from several args

From Dev

list as Python class variable

Related Related

  1. 1

    Create a parametrized List from a Class variable

  2. 2

    How to create new object of Generic Type T from a parametrized List<T>

  3. 3

    create and use class from variable

  4. 4

    create and use class from variable

  5. 5

    Can parameters from a parametrized class be used in external function definitions?

  6. 6

    Create list of class variables from list of objects

  7. 7

    Inheritance with parametrized class

  8. 8

    How to create a variable from CSS class attribute?

  9. 9

    How to create custom class from variable? (not Object)

  10. 10

    Create object from class stored in variable

  11. 11

    Python class variable referenced from within list

  12. 12

    Python class variable referenced from within list

  13. 13

    How to create case class from List[String]?

  14. 14

    Create a CSV of class members from a list<T>

  15. 15

    Parametrized object cannot be resolved to variable

  16. 16

    Having parametrized class in swagger annotations

  17. 17

    How set a variable in Mysql from Visual Basic .net using parametrized Value from a Texbox

  18. 18

    Create an dyanamic list from a List(of Class) with where condition

  19. 19

    create a string list from a generic class and and apply TrueForAll on that list

  20. 20

    Create an instance of a list of Class (1) that inherits from another list of class(2) with a list from the other (2)

  21. 21

    Swift 2.0: Parametrized classes don't call proper == function if it inherits from class that is Equatable

  22. 22

    Why is it possible to get back an object of "incorrect-type" from the parametrized List in Java?

  23. 23

    Create binned variable from results of class interval determination

  24. 24

    Can't create a variable in class that inherits from Atom api

  25. 25

    Create a class based on a variable

  26. 26

    Accessing Linked List private variable from outside class

  27. 27

    how to pass two variable values from a java util list class

  28. 28

    Shell: how to create a list variable from several args

  29. 29

    list as Python class variable

HotTag

Archive