Actually, there are two questions:
I want to know if it is possible to change the link for a list class during a loop, as shown below:
Yes, it is safe. And for security, I mean: changing the link does not interfere with an already running cycle.
An iterator gets reset and starts again for a new link?
No, the iterator will never reset. That 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. This makes it difficult to understand the code, and probably the result is not what you expect when you do it (for example, in your case, I realized that you expected the loop to begin re-evaluating the collection).
In your case, encapsulate the method and call it recursively in the subtree:
Tree findMinimalTree(Tree tree) { for (Tree st : tree.getSubtrees()) { if (condition) return findMinimalTree(st); } return tree; }
Filipe borges
source share