The wild card will mess you up here because it's not doing what you intend it to do. Wild cards are traditionally used when you need to refer to some set of generic generics.
For example, if you wanted to let the user give you a collection that contained any type of Object then your method could take a Collection<? extends Object>
as its parameter. You would want to do this because if you just used Collection<Object>
as the parameter then they couldn't pass a Collection<String>
.
So, the problem is that List<? extends Object>
has some limitations because the compiler cannot know what specific type that List takes. For example, it could be a List<String>
... the compiler has no idea. For that reason, you could never call any method on List that takes a <? extends Object>
as its parameter. In other words, you cannot call add().
You could call get()... because the compiler knows what type the receiving variable is and can check that it's ok. Object foo = myList.get(0) is fine.
I think in this case you really want List<Object>
and to make sure to declare the type parameter on the ArrayList as well.
List<Object> list = new ArrayList<Object>();
And if you want strings then you will have to specifically convert the Objects to string. I suggest String.valueOf() personally.
Incidentally, the above is documented pretty well in the wildcards section of the Generics chapter in Core Java volume 1 (well, at least my ancient JDK 1.5 edition).