Collection-based for loop syntax question.
In the code fragment for the listAll() method presented on bottom of page 582, Chapter 13, the following code line is used:
for(Object obj : list)
but the class type of input variable list is given as the wildcard specification:
LinkedList<?> list
How can obj, a variable of type Object, be the type identifier for a collection of type LinkedList<?>, which, presumably, can only hold a LinkedList type (where a collection type is substituted for the ? in <?>)?
On page 113 in Chapter 3, where the author first introduced the collection based for loop, he stated:
"The first element [of the for loop] is an identifier of the type that you specify, and the second is an expression specifying a collection of objects or values of the specified type."
It looks like we have a type mismatch here, between the identifier and the collection of the specified identifier type. I realize that type Object can store any other type, and perhaps this syntax works, but nowhere in the book did the author introduce or justify the above syntax. I am still in Chapter 13, but I did peruse Chapter 14 (on Collections), as well as the Index, and I cannot find any further information on this syntax.
Anyone who can shed some light on this topic will be greatly appreciated. If necessary, I will post the result to the Errata cite.
Thanks,
forger
|