This is because the index is not accessible using syntax foreach. You should use traditional iteration if you need an index:
for (int i =0; i < names.length; i++) {
String name = names[i];
}
If you do not need an index, a standard one foreachwill be enough:
for (String name : names) {
}
EDIT : obviously, you can get the index using a counter, but then you have a variable available outside the scope of the loop, which I find undesirable
source
share