我在Java中有一个ArrayList。现在,我想访问指向所述列表的指针。这样一来,我就可以在指针之外制作变量并使用它进行操作。任何建议如何做到这一点?

我想做的完全类似于使下面的“列表”始终指向“ someOtherList”的值。

import java.util.ArrayList;
import java.util.List;

public class ListContainingObject {

    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public static void main(String args[]){
        ListContainingObject listContainingObject= new ListContainingObject();

        System.out.println(listContainingObject.getList());

        List someOtherList = new ArrayList();
        listContainingObject.setList(someOtherList);
        System.out.println(listContainingObject.getList());
        System.out.println(someOtherList);

        someOtherList.add("1");
        System.out.println(listContainingObject.getList());
        System.out.println(someOtherList);

        //I want the two below to have the same value
        someOtherList = new ArrayList();
        System.out.println(listContainingObject.getList());
        System.out.println(someOtherList);
    }
}


似乎合适的解决方法是再次像下面一样显式地调用设置器。

public class ListContainingObject {
    public void resetList(List toReset) {
        this.list = new ArrayList();
        toReset = this.list;
    }
}
listContainingObject.resetList(someOtherList);


但这会导致另一个问题,其中我希望下面的solcowiab.getList()和listContainingObject.getList()始终相同,假设我没有SomeOtherListContainingObjectWhichIsABlackBox的源代码。

import java.util.ArrayList;
import java.util.List;

    public class ListContainingObject {

    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public static void main(String args[]) {
        ListContainingObject listContainingObject = new ListContainingObject();
        SomeOtherListContainingObjectWhichIsABlackBox solcowiab = new SomeOtherListContainingObjectWhichIsABlackBox();
        List aNewList = new ArrayList();
        aNewList.add("1");
        solcowiab.setList(aNewList);
        listContainingObject.setList(solcowiab.getList());
        System.out.println(listContainingObject.getList());
        System.out.println(solcowiab.getList());

        //The two below will have the same value but
        //at some point "list" did not point to "someOtherList"
        solcowiab.aMethodThatSupposedlyWontCallSetList();
        listContainingObject.setList(solcowiab.getList());
        System.out.println(listContainingObject.getList());
        System.out.println(solcowiab.getList());
    }
}

class SomeOtherListContainingObjectWhichIsABlackBox {

    private List someOtherList;

    public List getList() {
        return someOtherList;
    }

    public void setList(List list) {
        this.someOtherList = list;
    }

    public void aMethodThatSupposedlyWontCallSetList() {
        //this one won't be observed by ListContainingObject
        setList(new ArrayList());
        getList().add("2");
        //do some other stuff

        //only this assignment will be controlled by ListContainingObject's main method
        setList(new ArrayList());
    }
}

最佳答案

您不能,因为行someOtherList = new ArrayList();someOtherList分配了一个全新的“内存部分”,这意味着它将指向与listContainingObject.getList()不同的内存位置。

现在确实是listContainingObject仅具有对您创建的列表的引用,但是此引用在语义上没有链接到someOtherList。因此,如果someOtherList更改,您的对象将不知道它。

如果要清除列表而不破坏链接,请使用someOtherList.clear()

编辑:您也许能够以另一种方式重置列表:

public class ListContainingObject {
    public void resetList(List toReset) {
        this.list = new ArrayList();
        toReset = this.list;
    }
}
listContainingObject.resetList(someOtherList);


但是,这是一个相当肮脏的技巧。但是,如果不以某种方式手动更改两个变量以引用内存中的新结构,我将无法获得自动更新另一个变量的方法。

10-08 12:15