7

I couldn't find any topic about this. I want to know if it is safe to change the reference for the list class during a loop like the one bellow:

Tree minimalTree = someTree;
for (Tree st : minimalTree.getSubtrees()) {
  if (condition)
    minimalTree = st;
}

Does the iterator gets reset and starts again for the new reference?

Edit: I forgot to say: this code is suited for situations where I want to narrow down the search for elements in the tree, let's say, the smaller tree that contains certain elements. In this case, it would be faster to keep looking only for the inner structures of "minimalTree" instead of the entire "someTree" structure.

4
  • 1
    What happens when you test it? Commented Feb 24, 2015 at 21:04
  • You should probably use a recursive call for that. Commented Feb 24, 2015 at 21:16
  • It seemed to work fine until now but I'm not really sure if it will work for massive processing. But maybe @biziclop is right and this kind of coding should be avoided... Commented Feb 24, 2015 at 21:19
  • The question is already answered, and recommendations about alternative implementations would probably not count as an "answer", so just a comment: In contrast to the recommendation that was given here: You should NOT solve this with a recursive call - at least not without taking into account that you may easily run into a StackOverflowError even for moderately large trees. Commented Feb 25, 2015 at 10:53

2 Answers 2

6

No, the iteration won't be reset. According to the JLS:

The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {
     {VariableModifier} TargetType Identifier = (TargetType) #i.next();
     Statement
}

The definition makes it obvious that the iterator is only initialised once, before the first iteration of the loop.

The behaviour when iterating across an array with an enhanced for statement is similar in this respect.

However I'd personally consider it poor practice as it makes the code hard to understand.

Sign up to request clarification or add additional context in comments.

Comments

2

Actually, there are two questions:

I want to know if it is safe to change the reference for the list class during a loop like the one bellow:

Yes, it is safe. And for safe I mean: changing the reference does not interfere with the already running loop.

Does the iterator gets reset and starts again for the new reference?

No, the iterator is never reset. It would be exactly the opposite of safe.

In my opinion, it is not a good practice to change iterator or collection variables inside a loop. It makes the code harder to understand, and probably the result is not what you expect when you do that (like in your case I understood you expected the loop to start over reevaluating the collection).

In your case, encapsulate in a method and recursively call it on subtree:

Tree findMinimalTree(Tree tree) {
   for (Tree st : tree.getSubtrees()) {
     if (condition)
       return findMinimalTree(st);
   }
   return tree;
}

5 Comments

I disagree. Read biziclop's answer. In my words, minimalTree.getSubtrees()[.iterator()] gets evaluated exactly once when entering the loop. Afterwards, you can let point the minimalTree reference to whatever you want. Keep in mind the assigment minimalTree = st does not change the object previously referenced by minimalTree (a thing C++ would do here). It just changes the reference minimalTree, i. e. the object minimalTree points to.
Yes, I understand your point of view. In my opinion the problem are (i) that at a first glance it seems to work like the c++ counterpart, and (ii) when you debugs you cannot see the collection you are iterating over anymore.
"It seems to work" is not the appropriate way to express this, I will try to improve it. Any suggestions?
The more i think of your answer, I get new ways to intepret it. Did you want to say sth. like: "The assignment "minimalTree = st;" is legal; but the iterator still iterates over the original collection; this does not seem what you expect." ?
I changed again, review please. If you want to help, i can turn it in a wiki.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.