Deleting objects from an ArrayList in Java

前端 未结 13 2024
轮回少年
轮回少年 2020-12-15 03:14

I need to delete some objects from an ArrayList if they meet a condition and I\'m wondering which way could be more efficient.

Here\'s the situation: I

13条回答
  •  没有蜡笔的小新
    2020-12-15 04:06

    Unless you're positive that the issue you're facing is indeed a bottleneck, I would go for the readable

    public ArrayList filterThings() {
    
        ArrayList pileOfThings;
        ArrayList filteredPileOfThings = new ArrayList();
    
        for (Thing thingy : pileOfThings) {
            if (thingy.property != 1) {
                filteredPileOfThings.add(thingy);
            }            
        }
        return filteredPileOfThings;
    }
    

提交回复
热议问题