I have the next code: a pojo class(Bean.java)
public class Bean {
private int id;
private String nom;
public Bean(int id, String nom) {
this.id = id;
this.nom = nom;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
}
and a app class that extends application this class is executed firsly (AppVariables.java)
public class AppVariables extends Application {
private ArrayList<Bean> str;
@Override
public void onCreate() {
super.onCreate();
str = new ArrayList<>();
str.add(new Bean(0, "java"));
str.add(new Bean(0, "jelly"));
str.add(new Bean(0, "hot"));
str.add(new Bean(0, "weird"));
}
public ArrayList<Bean> getListStr() {
return str;
}
public ArrayList<Bean> getClone() {
ArrayList<Bean> r = new ArrayList<>(2);
r.add(str.get(2));
r.add(str.get(3));
return r;
}
}
and my main activity that launches (MainActivity.java)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppVariables app = (AppVariables) getApplicationContext();
ArrayList<Bean> beans = app.getListStr();
beans.get(0).setNom("C++");
String r = "";
for (Bean b : beans) {
r += b.getNom() +"\n";
}
Toast.makeText(this, r, Toast.LENGTH_LONG).show();
ArrayList<Bean> clone = app.getClone();
beans.get(3).setNom("HOT");
r = "";
for (Bean b : clone) {
r += b.getNom() +"\n";
}
Toast.makeText(this, r, Toast.LENGTH_LONG).show();
}
}
The question is: Why when I read the ArrayList str
, it is modified? I never manipulate its values, I only modified the value of the local var "beans".
The two Toast
show me the same string.
I recently create a clone method getClone() but when I modified the ArrayList bean it still modify the "cloned array"