关于List中引用的问题

来源:互联网 发布:阿里云香港ss 10块 编辑:程序博客网 时间:2024/06/06 00:22

HashMap map1 = new HashMap();

HashMap map2 = list.get(0);//List中元素为map

map2=map1;

则list中元素没有改变,因为语句map2=map1 只是改变了map2的引用指向并没有改变list.get(0)的引用指向。

HashMap map2 = list.get(0);//本条语句是重新分配了一块内存(map2 )指向与list.get(0)相同的地址。

原创粉丝点击