Generics and the question mark
generics, java
Solution
From the Generics Tutorial. Thanks to Michael's answer!
It isn't safe to add arbitrary objects to it however:
Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
Since we don't 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't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.
Problem
I'd like to use a generic list, but the initialization method only returns a `List`. The following code works well: ``` List tmpColumnList = aMethodToInitializeTheColumnList(); tmpColumnList.add("ANICELITTLECOLUMN"); ``` Java accuses that I'm using a raw type and I should paramerize the list. So I added the question mark parameterize this list. ``` List<?> tmpColumnList = aMethodToInitializeTheColumnList(); tmpColumnList.add("ANICELITTLECOLUMN"); ``` Problem is: Now the `add(..)` method doesn't work anymore. I cannot assure that the list only contains `String`s as `aMethodToInitializeTheColumnList()` is not implemented in my code. What is my mistake? Thanks!