storing objects in List<?>

I've created a List<?> list; but when I want to add any object for ex. list.add(new Integer(5)); there's an error 'The method add(capture-of ?) in the type List<capture-of ?> is not applicable for the arguments (Integer)'How should I add objects to this kind of
[297 byte] By [lasjaka] at [2007-11-27 6:50:33]
# 1

From Sun's 'Generics in the Java Programming Language'

Collection<?> c = new ArrayList<String>();

c.add(new Object()); // compile time error

Since we don抰 know what the element type of c stands for, we cannot add objects

to it. The add() method takes arguments of type E, the element type of the collection.

When the actual type parameter is ?, it stands for some unknown type. Any parameter

we pass to add would have to be a subtype of this unknown type. Since we don抰 know

what type that is, we cannot pass anything in. The sole exception is null, which is a

member of every type.

anand_nalyaa at 2007-7-12 18:24:36 > top of Java-index,Core,Core APIs...
# 2
Ok. I've tried changing List type to List<? extends Integer> but it still doesn't work. What is the point of this <?> if we can't use it? :/
lasjaka at 2007-7-12 18:24:36 > top of Java-index,Core,Core APIs...
# 3
But you can use it. You just can't add any entries to a List declared that way. You can certainly extract entries from such a List.
DrClapa at 2007-7-12 18:24:36 > top of Java-index,Core,Core APIs...
# 4
OK. Thanks for help
lasjaka at 2007-7-12 18:24:36 > top of Java-index,Core,Core APIs...