+ 2
What is an Enhanced Loop in Java?
2 Antworten
+ 3
String[] myList = {Element1,Element2,Element3,Element4};
for (String element : myList) {
System.out.println(element);
}
+ 2
It's
for (DataType varName : list)
Say it like 'for each DataType in list'.
It's basically the same as writing this:
for(int i = 0; i < list.length; i++)
DataType varName = list[i];
Except using the Iterator class it pulls out each element one by one for you, of the type you specify.
Performance wise i'm pretty sure it does better when casting is needed. Otherwise it's not much different, if not worse than the standard loop.