Now someone told me that this is not very good programming because I use the return statement inside a loop and this would cause garbage collection to malfunction.
That's incorrect, and suggests you should treat other advice from that person with a degree of skepticism.
The mantra of "only have one return statement" (or more generally, only one exit point) is important in languages where you have to manage all resources yourself - that way you can make sure you put all your cleanup code in one place.
It's much less useful in Java: as soon as you know that you should return (and what the return value should be), just return. That way it's simpler to read - you don't have to take in any of the rest of the method to work out what else is going to happen (other than finally
blocks).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…