小编Roh*_*kar的帖子

如果我们在父List中添加元素,为什么JDK会向UnmodifyableList添加一个元素

 List<Person> intList = new ArrayList<Person>();
        // creating Parent List
        // Person Class contains two fields Name and Age
        Person p1 = new Person("James", 28);
        Person p2 = new Person("Duncan", 26);
        Person p3 = new Person("Lukan",32);
        Person p4 = new Person("Therry", 12);
        // Creating ParentList
        intList.add(p1);
        intList.add(p2);        
        intList.add(p3);
        intList.add(p4);

        ImmutableList<Person> immutableList = ImmutableList.copyOf(intList);        
        List<Person> unModifybale = Collections.unmodifiableList(intList);
        //Adding element to parentList
        intList.add(p4);        

        System.out.println("\n" +intList);  

        // Here unModifyble List also gets element after added in parent list
        // why does it happen …
Run Code Online (Sandbox Code Playgroud)

java collections

1
推荐指数
1
解决办法
53
查看次数

标签 统计

collections ×1

java ×1