Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic, iteratorable ListModels and foreach loops

 
Stevo Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone!

I am writing my own ListModel that supports generics and provides an API to ArrayLists. I decided to implement the Iterable interface so I could use foreach loops on my ListModels but I'm running into trouble.

My iterator implementation in MyListModel looks like this:




In my test code I am able to write this fine:



But when I switch to a foreach loop I get errors:



Namely this one: "Error: Type mismatch: cannot convert from element type Object to Integer"


What I've been reading about the foreach loop suggests that if the top one works the bottom one should to. Anyone know what I am doing wrong here?
 
Stevo Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solved my problem!

When declaring the class you must specify the generic behind the "implements Iterable"

i.e.



Originally I forgot the <E> after Iterable in the class declaration.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic