Is it possible to get the index of a for-each loop in Java

Viewed 6563

Given the code below, is it possible to remove the index of p from the properties list using this style of for loop in Java?

List<Properties> propertiesList = new ArrayList<Properties>();
String keyToDelete = "blah";

for(Properties p : propertiesList) {
    if(p.getKey().equals(keyToDelete)) {
        propertiesList.remove(index) //How to remove the element at index 'p'
    }
}

This is how i would accomplish this with the other for loop

List<Properties> propertiesList = new ArrayList<Properties>();
String keyToDelete = "blah";

for(int i = 0; i < propertiesList.size(); i++) {
        if(p.getKey().equals(keyToDelete)) {
                propertiesList.remove(i);
        }
}
5 Answers
Related