ArrayList问题:一个值重置所有值

时间:2017-04-02 21:07:35

标签: java android

我的应用程序包含ArrayList ArrayListsArrayList<ArrayList<Element>>)。基本上,每个元素都是用户可以与之交互的项目。每次与元素交互时,都会更新该元素,然后将整个容器对象重新保存到文件系统。一种类型的元素(extends Element)是EBoolean,它可以包含元素标题,true或false值以及其他几个常规参数。调用updateBoolean方法时,会向其传递制表符,ID和布尔值。该方法扫描正确的元素(匹配ID)并更新其值。但是,我得到了一些奇怪的行为,修改一个元素会修改整个数组中的每个元素。

这里是输出代码:

    public void updateBoolean(int index, int ID, boolean b) {
    // Data that is gonna be saved
    System.out.println("Statistics: Index: "+index+" Array # "+elements.size()+" Elements in array #: "+elements.get(index).size());
    for (int i = 1; i < elements.size(); i++) {
        System.out.println("TAB " + i + " Title: " + elements.get(i).get(0).getTitle() + "Boolean value: " + ((EBoolean) elements.get(i).get(0)).getValue());
    }

    for (int j = 0; j < elements.get(index).size(); j++) {
        if (elements.get(index).get(j).getID() == ID) {
            EBoolean ent = (EBoolean) elements.get(index).get(0);
            ent.setValue(b);
            ent.setModified(true);
            elements.get(index).set(j, ent);
            System.out.println("Updated booleans, here's what we have now: ");
            for (int i = 1; i < elements.size(); i++) {
                System.out.println("TAB " + i + " Title: " + elements.get(i).get(0).getTitle() + "Boolean value: " + ((EBoolean) elements.get(i).get(0)).getValue());

            }
            return;
        }
    }

}

输出:

04-02 16:01:05.441 15442-15442/? I/System.out: Statistics: Index: 4 Array #         13 Elements in array #: 4
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 1 Title: BooleanBoolean    value: true
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 2 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 3 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 4 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 5 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 6 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 7 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 8 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 9 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 10 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 11 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 12 Title: BooleanBoolean value: false
04-02 16:01:05.441 15442-15442/? I/System.out: Updated booleans, here's what we have now: 
04-02 16:01:05.441 15442-15442/? I/System.out: TAB 1 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 2 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 3 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 4 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 5 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 6 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 7 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 8 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 9 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 10 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 11 Title: BooleanBoolean value: true
 04-02 16:01:05.441 15442-15442/? I/System.out: TAB 12 Title: BooleanBoolean value: true

1 个答案:

答案 0 :(得分:0)

添加到阵列时未创建新实例。确保ArrayList<ArrayList<Element>>中的每个对象都是它自己的实例。