I know it’s a stupid question, since I’ve never seen any example that using method returns list in the for each loop, I intuitively feel that is not a good idea:
for (String element : computeElementList()) {
Systems.out.print(element);
}
should I first assign the method to a variable? Or is this not necessary at all, because this computeElementList() method will just be called one time at the beginning of the loop?
The extra variable is not necessary because
computeElementList()will just be called just once at the beginning of the loop.Unless you need to the result of
computeElementList()for something else, creating a new variable would just add clutter to the code (IMO).