• Post Reply Bookmark Topic Watch Topic
  • New Topic

Command pattern: Iterating over list of objects  RSS feed

 
Daena Buyasta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid I've got lots of questions today. Following on from here here.

I'm using a LinkedList (correct choice?) to store a queue of instructions for my robot. The instructions are represented as objects like;



These get loaded into the LinkedList with the add method then I'm trying to read them out of the queue in the robots main run method like;



That results in a complain about "incompatible types" on this line "RobotInstruction Ri = It.next();".

How do you deal with this one? The examples I've found via Google so far have all involved casting the object returned from the Iterator to a String.

Many thanks again.
[ August 20, 2004: Message edited by: Daena Buyasta ]
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iterator returns Object when you call it.next(). You need to cast this into the appropriate class. In your case, casting it to
should work fine.
 
Daena Buyasta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect. Many thanks!

Have to say think RoboCode is a brilliant idea - this is the first I'm I've ever felt really motivated to do stuff with Java.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe LinkedList is usually the best Collection to use for a queue. Jakarta Commons Collections offers a Queue class, amongst others, if you want a closer fit.

Jules
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!