Don’t use a for loop with an index (or counter) variable if you can replace it with the enhanced for loop (since Java 5) or forEach (since Java 8). It’s because the index variable is error-prone, as we may alter it incidentally in the loop’s body, or we may starts the index from 1 instead of 0.Consider the following example that iterates over an array of Strings:12345String[] names = {"Alice", "Bob", "Carol", "David", "Eric", "Frank"};for (int i = 0; i < names.length; i++) { doSomething(names[i]);}As you can see, the index variable i in this for loop can be altered incidentally which may cause unexpected result. We can avoid potential problems by using an enhanced for loop like this:123for (String aName : names) { doSomething(aName);}This does not only remove potential issues but also make the code cleaner and more succinct.