go to previous page   go to home page   go to next page

Answer:

ArrayList<int> intList = new ArrayList<int>();

No. Generics do not work with primitive types.


Objects Only

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. The bookcase is like a program, a shelf is like an array, and the shelf label is like a type variable. The individual books are the objects of different types.


QUESTION 3:

(Thought Question:) Is the following statement likely to work?

ArrayList<String> names = new ArrayList<String>();
names.add( 34 );

go to previous page   go to home page   go to next page