arraylist最终解决方案,以便可以添加项目

问题描述 投票:0回答:2

我试图将Clothing对象从另一个类中的重写方法添加到arraylist,这迫使它被声明为final。但是,当一个变量被声明为final时,它无法更改,但这正是我需要做的。这是代码:

final List<Clothing> clothingItems = new ArrayList<>();
mClothingRef.addChildEventListener(new ChildEventListener() {
    public void onChildAdded(DataSnapshot snapshot, String s) {
        Clothing clothing = snapshot.getValue(Clothing.class);
        clothingItems.add(clothing);
    }
}

我如何解决clothingItems是最终的事实,以便clothingItems列表填充Clothing对象?

java interface final
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.