• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can "enhanced for" loop through an array of object references?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. >.>b Another newbie here like this one; also studying Head First Java (second edition), also hoping to write apps for my new Android phone.

I just finished chapter five, with the enhanced for loops and things, and I'm trying to write a test program that incorporates them. Only problem is, I can't seem to get it to do an enhanced for loop through an array of object references. Here's the (slightly frivolous) code example:



I'm trying to get it to execute the loop's instructions on each Pokemon() object that's part of the redTeam[] array, but the program's output indicates that it's skipping over the loops entirely (the getHP() and setHP() methods in the Pokemon() object both generate output if they execute). Internet searching hasn't turned up a solution. Can enhanced for loops work on arrays of object references and I'm just doing it wrong, or should I use a different kind of loop?
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The inside of this loop never executes because the condition "redTeamFaint = false" will always be false. You've accidentally used "=" (which is the assignment operator) instead of "==" which is a comparison operator. The expression "redTeamFaint = false" assigns false to redTeamFaint, and then returns false, so the loop never executes.

For this reason, it's a very bad idea to ever compare to a boolean literal. Instead if "redTeamFaint == false" you could just write "!redTeamFaint" (read this as "while not redTeamFaint" or "while redTeamFaint ain't true").

Most people make this mistake once in their career, so consider yourself to have passed an important milestone!
 
Kayton Fox
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, many thanks! I corrected that and it compiled successfully. ^.^ They haven't taught me how to do !statements yet in Head First Java, so I wasn't aware of that trick.

I'll probably be back here soon, so hello to everyone and I hope you're all having fun with your own code!
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
Most people make this mistake once in their career, so consider yourself to have passed an important milestone!

Indeed. I love this sentence
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is exactly the reason why you shouldn't use "== true" or "== false". Instead of "b == true" simply write "b"; instead of "b == false" simply write "!b".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!