java for循环 去除为空项

List<T> photos = new ArrayList<>();
int size = photos.size();
for(int i = size - 1; i >= 0; i--){
    if(photos.get(i) == null){
    photos.remove(i);
    }
}

移除list中某空项时,for循环从list最后一项开始遍历,去除。不然可能出现脚标异常

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0