首页 > 其他 > 详细

将map中的value赋值给list,list改变为什么会引起map也变呢?

时间:2020-01-19 17:37:23      阅读:170      评论:0      收藏:0      [点我收藏+]
Map<Integer, List<Integer>> map1 = new HashMap<>();
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
        for (int i = 1; i < 6; i++) {
            list1.add(i);
        }
        for (int k = 1; k < 6; k++) {
            map1.put(k, list1);
        }
        System.out.println(map1);
        list2 = map1.get(1);
        list2.remove(0);
        System.out.println(map1);

 

 

 

技术分享图片

 

 

map除了基本类型是正儿八经的值传递,其余的都是引用地址传递

由于map的value存储的引用地址传递(list),所以当list对象的内部属性发生改变时,map中的value随着改变

将map中的value赋值给list,list改变为什么会引起map也变呢?

原文:https://www.cnblogs.com/cjeandailynotes/p/12214328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!