• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange "for" behaviour in Java 1.5

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.

I use Java 1.5 for a while and i used new-style "for" to operate collections serveral times. But recently I had received a compilation error, and I don't know why.
If I rewrite cycle using old good interators - all works fine.
Here is code:

Code, that works:



Code, that doesn't work:




Compilation error in the line "for (Map.Entry..."
Error: line (148)incompatible types
found : java.lang.Object
required: java.util.Map.Entry

Please, tell me what's wrong. May be it's me, compilator or smth. elese
I use Sun JDK 5.0 Update 3, IntelliJ IDEA 5.0 (build 3467)
Thanks.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two types for the generic MAP, one for the key and one for the contents, so your declaration should show both types. If you want to keep the same code, you need to cast to the type of the stored value after getValue( key ). Try this


[ November 13, 2005: Message edited by: Mike Rainville ]
 
intenter
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
I fixed problem using

instad of


Generics is very powerful tool, but it's not easy to understand their logic in cases like this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic