I have the following code
final List<ItemDto> itemDtos = result.getResults();
List<String> deleteItemIds = new ArrayList<String>();
// remove items which have been deleted on the server
for(ItemDto itemDto : itemDtos) {
if (itemDto.getIsRemoved()) {
deleteItemIds.add(itemDto.getId());
itemDtos.remove(itemDto); // TODO not sure if this works here
}
}
I iterate over the itemDtos and remove an ItemDto at the last operation in the for loop. I think that this works if I do not need something like itemDtos.indexOf(o)
inside the loop.
Is the code I did correctly working?
Edit: Here is a second variant:
List<ItemDto> itemDtos = result.getResults();
List<ItemDto> tmpList = itemDtos;
List<String> deleteItemIds = new ArrayList<String>();
// remove items which have been deleted on the server
for(ItemDto itemDto : tmpList) {
if (itemDto.getIsRemoved()) {
deleteItemIds.add(itemDto.getId());
itemDtos.remove(itemDto); // TODO not sure if this works here
}
}
Is the second variant better?