ArrayList<int> intList = new ArrayList<int>();
No. int
is a primitive type. Generics do not work with primitive types.
The value of the type variable E
when an ArrayList<E>
is constructed must be a class name.
This is true for all generic classes.
The idea of a type variable might sound a little strange.
Think of a bookcase with several shelves.
Each shelf is labeled with the type of book it holds:
science fiction, fantasy, non-fiction, and reference.
A shelf is like an ArrayList
,
and the shelf label is like a type variable.
The individual books are the objects of different types.
(Thought Question:) Is the following statement likely to work?
ArrayList<String> names = new ArrayList<String>();
names.add( 34 );