快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

JAVA:List复制-创新互联

ArrayList的复制方法有几种,我们这边分别列出来,并且判断了更改复制的List会不会对原List有影响

成都创新互联是专业的甘州网站建设公司,甘州接单;提供成都网站制作、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行甘州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
初始List
ArrayListpre = new ArrayList<>();
pre.add(1);
pre.add(2);
pre.add(3);
1、new ArrayList<>(pre)
System.out.println("=================方法一new ArrayList<>(pre)===================");
        ArrayListafter = new ArrayList<>(pre);
        //输出after
        System.out.println("=========输出after=========");
        System.out.println(after);
        System.out.println("====输出两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
//        向after中添加新元素
        after.add(4);
        System.out.println("=========输出新pre、after=========");
        System.out.println("pre" + pre);
        System.out.println("after" +after);
        System.out.println("====输出更改后两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
=================方法一new ArrayList<>(pre)===================
=========输出after=========
[1, 2, 3]
====输出两个list的hashcode====
pre的hashcode:30817
after的hashcode:30817
=========输出新pre、after=========
pre[1, 2, 3]
after[1, 2, 3, 4]
====输出更改后两个list的hashcode====
pre的hashcode:30817
after的hashcode:955331
2、after = pre
System.out.println("=================方法二 after= pre ===================");
        ArrayListafter = new ArrayList<>();
        after = pre;
        //输出after
        System.out.println("=========输出after=========");
        System.out.println(after);
        System.out.println("====输出两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
//        向after中添加新元素
        after.add(4);
        System.out.println("=========输出新pre、after=========");
        System.out.println("pre" + pre);
        System.out.println("after" +after);
        System.out.println("====输出更改后两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
=================方法二 after= pre ===================
=========输出after=========
[1, 2, 3]
====输出两个list的hashcode====
pre的hashcode:30817
after的hashcode:30817
=========输出新pre、after=========
pre[1, 2, 3, 4]
after[1, 2, 3, 4]
====输出更改后两个list的hashcode====
pre的hashcode:955331
after的hashcode:955331
3、pre.clone();
System.out.println("=================方法三 pre.clone() ===================");
        ArrayList after = (ArrayList) pre.clone();
//        after = pre;
        //输出after
        System.out.println("=========输出after=========");
        System.out.println(after);
        System.out.println("====输出两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
//        向after中添加新元素
        after.add(4);
        System.out.println("=========输出新pre、after=========");
        System.out.println("pre" + pre);
        System.out.println("after" +after);
        System.out.println("====输出更改后两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
=================方法三 pre.clone() ===================
=========输出after=========
[1, 2, 3]
====输出两个list的hashcode====
pre的hashcode:30817
after的hashcode:30817
=========输出新pre、after=========
pre[1, 2, 3]
after[1, 2, 3, 4]
====输出更改后两个list的hashcode====
pre的hashcode:30817
after的hashcode:955331

Process finished with exit code 0
4、after.addAll(pre)
System.out.println("=================方法四 after.addAll(pre) ===================");
        ArrayListafter = new ArrayList<>();
        after.addAll(pre);
        //after.allAll(pre) == ArrayListafter = new ArrayList<>(pre);
        //输出after
        System.out.println("=========输出after=========");
        System.out.println(after);
        System.out.println("====输出两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
//        向after中添加新元素
        after.add(4);
        System.out.println("=========输出新pre、after=========");
        System.out.println("pre" + pre);
        System.out.println("after" +after);
        System.out.println("====输出更改后两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
=================方法四 after.addAll(pre) ===================
=========输出after=========
[1, 2, 3]
====输出两个list的hashcode====
pre的hashcode:30817
after的hashcode:30817
=========输出新pre、after=========
pre[1, 2, 3]
after[1, 2, 3, 4]
====输出更改后两个list的hashcode====
pre的hashcode:30817
after的hashcode:955331
5、 for(int i in pre)
System.out.println("=================方法五 for(int i in pre) ===================");
        ArrayListafter = new ArrayList<>();
        for (int i: pre) {after.add(i);
        }
        //输出after
        System.out.println("=========输出after=========");
        System.out.println(after);
        System.out.println("====输出两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
//        向after中添加新元素
        after.add(4);
        System.out.println("=========输出新pre、after=========");
        System.out.println("pre" + pre);
        System.out.println("after" +after);
        System.out.println("====输出更改后两个list的hashcode====");
        System.out.println("pre的hashcode:" + pre.hashCode());
        System.out.println("after的hashcode:" + after.hashCode());
=================方法五 for(int i in pre) ===================
=========输出after=========
[1, 2, 3]
====输出两个list的hashcode====
pre的hashcode:30817
after的hashcode:30817
=========输出新pre、after=========
pre[1, 2, 3]
after[1, 2, 3, 4]
====输出更改后两个list的hashcode====
pre的hashcode:30817
after的hashcode:955331

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前标题:JAVA:List复制-创新互联
文章转载:http://6mz.cn/article/hpcjs.html

其他资讯