网站建设资讯

NEWS

网站建设资讯

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://cdweb.net/article/hpcjs.html