首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否可以在Java中获取for-each循环的索引

是否可以在Java中获取for-each循环的索引
EN

Stack Overflow用户
提问于 2012-07-19 23:33:50
回答 4查看 6.5K关注 0票数 17

根据下面的代码,有没有可能使用这种风格的p循环从属性列表中删除for的索引?

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'
    }
}

这就是我如何使用另一个for循环来实现这一点

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);
        }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11564262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档