1

I always have had trouble translating an enhanced for loop into a standard for loop. Here is the enhanced for loop that I want to change into a standard for loop structure:

for (Thing cp: getPerson().getEnemy().getMinions()) { if (cp.canMoveTo(this.getLocation()) && cp.getLocation() != null) { return true; } } return false; } 

How can I change this into a standard for loop?

1
  • 1
    What is the return type of getMinions()? Commented Mar 21, 2015 at 14:06

1 Answer 1

3

The enhanced for statement can loop through arrays and any kind of Iterable object, so the answer depends on whether getMinions returns an array or an Iterable object.

If it returns an array, then:

Thing[] minions = getPerson().getEnemy().getMinions(); for (int i = 0; i < minions.length; ++i) { Thing cp = minions[i]; // ... } 

If it returns an Iterable, then:

Iterator<Thing> it = getPerson().getEnemy().getMinions().iterator(); while (it.hasNext()) { Thing cp = it.next(); // ... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.