Is there a way to create a list of primitive int or any primitives in java
No you can't. You can only create List of reference types, like Integer
, String
, or your custom type.
It seems I can do List myList = new ArrayList();
and add "int" into this list.
When you add int
to this list, it is automatically boxed to Integer
wrapper type. But it is a bad idea to use raw type lists, or for any generic type for that matter, in newer code.
I can add anything into this list.
Of course, that is the dis-advantage of using raw type. You can have Cat, Dog, Tiger, Dinosaur, all in one container.
Is my only option, creating an array of int and converting it into a list
In that case also, you will get a List<Integer>
only. There is no way you can create List<int>
or any primitives.
You shouldn't be bothered anyways. Even in List<Integer>
you can add an int
primitive types. It will be automatically boxed, as in below example:
List<Integer> list = new ArrayList<Integer>();
list.add(5);