java ArratList深拷贝

引用深拷贝 便捷方法

class Test {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User(i, "name" + i);
            list.add(user);
        }
        ArrayList<User> list1 = new ArrayList<>(list.subList(0, list.size()));

        list1.remove(0);

        System.out.println("原:" + list);
        System.out.println("新:" + list1);

    }

    public static class User {
        String name;
        Integer age;

        public User() {
        }

        public User(Integer age, String name) {
            this.age = age;
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}

在这里插入图片描述

你可能感兴趣的:(java)