我有一个实现getter和setter方法以及相关代码的类,如下所示。

ArrayList<String> viewArray = new ArrayList<String>();

public ArrayList<String> getView() {
return viewArray;
}


从我的活动,我试图访问存储数组,如:

ArrayList<String> al = new ArrayList<String>();

al = parsedExampleDataSet.getView();


但是“ al”没有接收到数据。但是,执行getView()时,将正确填充viewArray。我想念什么?谢谢。

最佳答案

其他人则发表了一些很好的评论,但我认为我会按照我的理解逐步介绍代码。

public class SomeClass {
    // this is local to this class only
    ArrayList<String> viewArray = new ArrayList<String>();
    public void process() {
       // i'm guessing there is some sort of processing method that is called
    }
    public ArrayList<String> getView() {
       return viewArray;
    }
}


这是您的活动类,带有有关a1值的一些详细信息:

public class YourActivity {
    ArrayList<String> al = new ArrayList<String>();
    public void someMethod() {
        // here a1 will be the same blank List you initialized it with
        // unless someMethod() has been called before or a1 modified elsewhere
        al = parsedExampleDataSet.getView();
        // after the call to getView, a1 is now a reference to SomeClass.viewArray
        // the ArrayList that a1 was initialized with is then garbage collected
    }
}


请编辑您的问题,以解释更多您遇到的问题。

09-12 12:47