1

My IntelliJ IDE gives me 'for' loop replaceable with enhanced 'for' warning for a loop. As I know in Java the forEach loop is a syntactic sugar.

The inspection info for that is : Reports for loops which iterate over collections or arrays, and can be replaced with an enhanced for loop (i.e. the foreach iteration syntax) The loop is something like below

        List<Object> myList = ... ;
        for (int i = 0; i < myList.size(); i++) {
            Object o = myList.get(i);
            // Do things with the object o
            ...
        }
        

so how is the enhanced for loop which is down below is better in this case?

        List<Object> myList = ... ;
        for (Object o : myList) {
            // Do things with the object o
            ...
        }
mcvkr
  • 3,209
  • 6
  • 38
  • 63

0 Answers0