Initializing an ArrayList whose type is specified by the user

I would like to initialize an array whose type is specified by the user through the command line. That is, if the user enters "int", I would like:

ArrayList<Integer> res = new ArrayList<Integer>();

Similarly, if the user enters "String", I would like:

ArrayList<String> res = new ArrayList<String>();

Now, of course, I can do this, a bunch of if-else instructions, but I wonder if there is a smart way to do this. Assume that user-entered types are always primitive types, i.e. Int, string, boolean, etc.

+4
source share
1 answer

, < > , aka type erasure...

, ArrayList<Object> ... , , .

, ...

+2

Source: https://habr.com/ru/post/1674980/


All Articles