dunwu / javacore

☕ JavaCore 是对 Java 核心技术的经验总结。
https://dunwu.github.io/javacore/
Creative Commons Attribution Share Alike 4.0 International
3.26k stars 543 forks source link

# 描述错误 : Java 容器之 List 章节 #20

Closed Whojohn closed 3 years ago

Whojohn commented 3 years ago

错误页:

  1. https://github.com/dunwu/javacore/blob/master/docs/container/java-container-list.md

错误内容

  1. ArrayList 实现了 Cloneable 接口,支持深拷贝

    正确描述

  2. ArrayList 实现了 Cloneable 接口,默认为浅拷贝

实例代码

        ArrayList<List<Integer>> temp = new ArrayList<List<Integer>>() {{
            add(new ArrayList<Integer>() {{
                add(1);
            }});
            add(new ArrayList<Integer>() {{
                add(2);
            }});
            add(new ArrayList<Integer>() {{
                add(3);
            }});
        }};
        List<List<Integer>> temp2 = (List<List<Integer>>) temp.clone();
        System.out.println("Copy source must be as follow when copy is deepcopy");
        temp.forEach(System.out::println);
        temp.get(1).add(3);
        System.out.println("Copy source:");
        temp2.forEach(System.out::println);
dunwu commented 3 years ago

错误页:

  1. https://github.com/dunwu/javacore/blob/master/docs/container/java-container-list.md

错误内容

  1. ArrayList 实现了 Cloneable 接口,支持深拷贝

正确描述

  1. ArrayList 实现了 Cloneable 接口,默认为浅拷贝

实例代码

        ArrayList<List<Integer>> temp = new ArrayList<List<Integer>>() {{
            add(new ArrayList<Integer>() {{
                add(1);
            }});
            add(new ArrayList<Integer>() {{
                add(2);
            }});
            add(new ArrayList<Integer>() {{
                add(3);
            }});
        }};
        List<List<Integer>> temp2 = (List<List<Integer>>) temp.clone();
        System.out.println("Copy source must be as follow when copy is deepcopy");
        temp.forEach(System.out::println);
        temp.get(1).add(3);
        System.out.println("Copy source:");
        temp2.forEach(System.out::println);

谢谢指正,你是对的