• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();'
reply
    Bookmark Topic Watch Topic
  • New Topic