I see some kind of behavior that I can’t understand about, and wondered if anyone would be kind enough to explain exactly what was happening here. This is the code I have.
public class MyClass { ArrayList<String> myList = new ArrayList<String>(); public MyClass() { populate(); removeData(myList); } private void populate() { myList.add("Some data"); myList.add("Some more data"); myList.add("Even more data"); } private void removeData(ArrayList<String> list) { ArrayList<String> temp = new ArrayList<String>(); temp = list; temp.remove("Some data"); } }
Now for some reason, after running this code, the data is deleted from the ArrayList "myList". Why is this happening, although I only have to delete the data from the variable inside the "removeData" method, and not from the "myList" field?
Wodlo source share