前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数组的拷贝

数组的拷贝

作者头像
iiopsd
发布2022-12-23 08:47:05
4140
发布2022-12-23 08:47:05
举报
文章被收录于专栏:iiopsd技术专栏iiopsd技术专栏

数组的拷贝

背景

一个数组中有多个map,要重复操作在map中插入个随机数,把map存到新的数组中,发现会有相同的map

代码语言:javascript
复制
@Test
    public void test4() {
        Random r = new Random();
        List<Map<String, Object>> l = new ArrayList<>();
        List<Map<String, Object>> l1 = new ArrayList<>();
        Map<String, Object> m = new HashMap<>();
        m.put("a", "1");
        Map<String, Object> m1 = new HashMap<>();
        m1.put("a", "2");
        l1.add(m);
        l1.add(m1);
        for (int i = 0; i < 3; i++) {
            for (Map<String, Object> b : l1) {
                int t = r.nextInt(1000);
                b.put("c", t);

                System.out.println(t);
            }
            l.addAll(l1);
        }

        System.out.println(l);

    }

输出结果:

代码语言:javascript
复制
851
543
204
668
108
347
[{a=1, c=108}, {a=2, c=347}, 
 {a=1, c=108}, {a=2, c=347}, 
 {a=1, c=108}, {a=2, c=347}]

尽管每次插入的随机数不同,但是发现结果是重复的,因为循环操作了同一个数组l1,而数组l中保存的是l1的地址(l.addAll(l1);)导致最后的结果是3组相同的数据。

解决方案:新建一个数组l2去接收数组l1的数据,每次操作新数组,实现数据隔离

方案一:List.addAll()(浅拷贝)

代码语言:javascript
复制
List<Map<String, Object>> l2 = new ArrayList<>();
l2.addAll(l1);

方案二:使用List的构造方法(浅拷贝)

代码语言:javascript
复制
List<Map<String, Object>> l2 = new ArrayList<>(l1);
代码语言:javascript
复制
@Test
    public void test5() {
        Random r = new Random();
        List<Map<String, Object>> l = new ArrayList<>();
        List<Map<String, Object>> l1 = new ArrayList<>();
        Map<String, Object> m = new HashMap<>();
        m.put("a", "1");
        Map<String, Object> m1 = new HashMap<>();
        m1.put("a", "2");
        l1.add(m);
        l1.add(m1);
        for (int i = 0; i < 3; i++) {
            // 方案一
//            List<Map<String, Object>> l2 = new ArrayList<>();
//            l2.addAll(l1);
            // 方案二
            List<Map<String, Object>> l2 = new ArrayList<>(l1);

            
            for (Map<String, Object> b : l2) {
                int t = r.nextInt(1000);
                b.put("c", t);

                System.out.println(t);
            }

            l.addAll(l2);
        }

        System.out.println(l);

    }

输出结果还是重复的数据

代码语言:javascript
复制
751
812
509
957
512
874
[{a=1, c=512}, {a=2, c=874}, 
 {a=1, c=512}, {a=2, c=874}, 
 {a=1, c=512}, {a=2, c=874}]

打印JSONObject.toJSONString()打印结果会发现还是相同的地址引用

代码语言:javascript
复制
System.out.println(JSONObject.toJSONString(l));

结果:

代码语言:javascript
复制
[{"a":"1","c":995},{"a":"2","c":946},
 {"$ref":"$[0]"},{"$ref":"$[1]"},
 {"$ref":"$[0]"},{"$ref":"$[1]"}]

方案三:数据类型转换(深拷贝)

代码语言:javascript
复制
@Test
    public void test6() {
        Random r = new Random();
        List<Map<String, Object>> l = new ArrayList<>();
        List<Map<String, Object>> l1 = new ArrayList<>();
        Map<String, Object> m = new HashMap<>();
        m.put("a", "1");
        Map<String, Object> m1 = new HashMap<>();
        m1.put("a", "2");
        l1.add(m);
        l1.add(m1);
        for (int i = 0; i < 3; i++) {
            // 方案三,转成String,在转回List
            String str = JSONObject.toJSONString(l1);
            List<Map<String, Object>> l2 = JSONObject.parseObject(str, List.class);
            
            for (Map<String, Object> b : l2) {
                int t = r.nextInt(1000);
                b.put("c", t);

                System.out.println(t);
            }

            l.addAll(l2);
        }

        System.out.println(l);

    }

测试发现数据没有重复,实现数据隔离

代码语言:javascript
复制
941
685
901
129
449
516
[{"a":"1","c":941}, {"a":"2","c":685}, 
 {"a":"1","c":901}, {"a":"2","c":129}, 
 {"a":"1","c":449}, {"a":"2","c":516}]

当然还有其他序列化方式的深度拷贝都能实现数据隔离,之后在补充

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数组的拷贝
    • 背景
      • 解决方案:新建一个数组l2去接收数组l1的数据,每次操作新数组,实现数据隔离
        • 方案一:List.addAll()(浅拷贝)
        • 方案二:使用List的构造方法(浅拷贝)
        • 方案三:数据类型转换(深拷贝)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档