Why does one arraylist change when a copy of it is modified

后端 未结 6 1712
傲寒
傲寒 2020-12-11 08:52

Its probably a very simple one but its\' still confusing me!

import java.util.ArrayList;

public class Sample {
    ArrayList i = new ArrayLis         


        
6条回答
  •  醉梦人生
    2020-12-11 09:35

    You created a memory stage for j with ; j = new ArrayList<>();
    but then you said that let j refer to i's memory stage. So after j=i; any changes on i or j are going to effect both of them. Because they reference to the same objects.

提交回复
热议问题