小编Mel*_*ray的帖子

为什么“ArrayList.remove”同时在局部变量和 myApplicationClass 中删除?

我有一个 MyApplicationClass,我在其中存有一个 ArrayList,然后我在 MainActivity 中创建了一个变量 ArrayList 并将 MyApplciationClass 中的变量分配给它,最后我调用了局部变量的 remove 来删除一个值,但该值从 MyApplicationClass 和localvariable,这是可能的,因为我只是从 MyApplicationClass 检索列表我没有做任何其他事情?

这是我的代码:

我的应用程序类:

private ArrayList<String> text = new ArrayList<>();


public ArrayList<String> getText() {
    return text;
}

public void addTest(String input) {
    text.add(input);
}
Run Code Online (Sandbox Code Playgroud)

主要活动:

//When click on a button:

final MyApplicationClass myApplicationClass = (MyApplicationClass) getApplicationContext();

//I add some example values
myApplicationClass.addTest("1");
myApplicationClass.addTest("2");

//Here I retrieve the variable in MyApplicationClass to put in into a local variable:

ArrayList<String> testlocal = myApplicationClass.getText();

//And here I remove a value …
Run Code Online (Sandbox Code Playgroud)

java android arraylist

1
推荐指数
1
解决办法
139
查看次数

标签 统计

android ×1

arraylist ×1

java ×1