• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does an iterator need a cast for a generically defined list

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code example results in the following compiler error:



Why does the Iterator.next() method need a cast when the List associated with the Iterator is declared generically?
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:

The above code example results in the following compiler error:



Why does the Iterator.next() method need a cast when the List associated with the Iterator is declared generically?


It needs a cast, because you are not using generics for the iterator. Try...



Henry
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. I thought that since the List provides the Iterator it will returned a typed iterator.

 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:Thanks Henry. I thought that since the List provides the Iterator it will returned a typed iterator.



The iterator() method returns an object. It is you who declared the iterator reference and assigned it. There is no way to create a class, with a method, that returns an object, that can control how it will be assigned. If you want to create an iterator reference, that doesn't use generics, the compiler can't stop you. It just does what you want.

Henry
 
Sebanti Sanyal
Ranch Hand
Posts: 58
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of 'Type Erasure',JVM does not know at runtime that 'list' refers to a LinkedList of "Integer objects".
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here is one that has confused me again:



This works fine and produces the output 5234252342. How come i didnt have to cast the i.next() call?

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:Ok here is one that has confused me again:



This works fine and produces the output 5234252342. How come i didnt have to cast the i.next() call?



Ok i think in the compiler is not asking for a cast in the above example because the value is not being assigned to another reference. It just calls the toString() which will call the overriden toString() method of what ever instance the returned object is. right?
 
Sebanti Sanyal
Ranch Hand
Posts: 58
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right.'Integer val=i.next();' doesn't compile without the cast, but you may have 'Object val=i.next();'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic