ArrayList clone()– ArrayList深拷贝和浅拷贝

示例程序以ArrayList克隆方法为例。 学生对象上的ArrayList深层复制和浅层复制示例。

1.简介

ArrayList clone()– ArrayList深复制和浅复制ArrayList clone()方法用于创建list的浅表副本 。 在新列表中,仅复制对象引用。 如果我们在第一个ArrayList中更改对象状态,则更改后的对象状态也将反映在克隆的ArrayList中。

2. ArrayList克隆–浅拷贝示例

将字符串列表复制到花药列表的示例程序
clone()

 package com.java.w3schools.blog.arraylist;  import java.util.ArrayList;  import java.util.List;  /** 
  * 
  * Java ArrayList Clone Example 
  * 
  * @author Java8Example blog 
  * 
  */  public class ArrayListCloneExample { 
  public static void main(String[] args) { 
   ArrayList<String> list = new ArrayList<>(); 
   list.add( "one" ); 
   list.add( "two" ); 
   list.add( "three" ); 
   list.add( "four" ); 
   list.add( "give" ); 
   System.out.println( "Before clone : " + list); 
   ArrayList clonedLis = (ArrayList) list.clone();   
   System.out.println( "After clone : " +clonedLis); 
  }  } 

输出:

 Before clone : [one, two, three, four, give]  After clone : [one, two, three, four, give] 

3. ArrayList克隆–自定义浅复制示例

示例程序将ArrayList与自定义对象进行浅表复制。 对克隆对象的修改反映在原始对象中。 观察该程序的输出。

 package com.java.w3schools.blog.arraylist;  import java.util.ArrayList;  /** 
  * 
  * Java ArrayList Clone Example with custom object 
  * 
  * @author Java8Example blog 
  * 
  */  public class ArrayListShalloCloneExample { 
  public static void main(String[] args) { 
   ArrayList<Student> list = new ArrayList<>(); 
   list.add( new Student( 100 , "harish" )); 
   list.add( new Student( 101 , "Jayaram" )); 
   ArrayList<Student> clonedList = (ArrayList) list.clone(); 
   Student student = clonedList.get( 1 ); 
   student.setName( "Jhon" ); 
   System.out.println( "Cloned list : " + clonedList); 
   System.out.println( "Original list : " + list); 
  }  }  class Student { 
  private int id; 
  private String name; 
  public Student( int id, String name) { 
   super (); 
   this .id = id; 
   this .name = name; 
  } 
  public int getId() { 
   return id; 
  } 
  public void setId( int id) { 
   this .id = id; 
  } 
  public String getName() { 
   return name; 
  } 
  public void setName(String name) { 
   this .name = name; 
  } 
  @Override 
  public String toString() { 
   return "Student [id=" + id + ", name=" + name + "]" ; 
  }  } 

输出:

 Cloned list : [Student [id= 100 , name=harish], Student [id= 101 , name=Jhon]]  Original list : [Student [id= 100 , name=harish], Student [id= 101 , name=Jhon]] 

4. ArrayList克隆–深度复制示例

下面的程序创建对象的深层副本。 对克隆列表的修改不会影响原始列表。

 public class ArrayListDeepCloneExample { 
  public static void main(String[] args) { 
   ArrayList<Student> list = new ArrayList<>(); 
   list.add( new Student( 100 , "harish" )); 
   list.add( new Student( 101 , "Jayaram" )); 
   ArrayList<Student> clonedList = new ArrayList<>(); 
   Iterator<Student> it = list.iterator(); 
   while (it.hasNext()) { 
    Student s = it.next(); 
    Student newS = new Student(s.getId(), s.getName()); 
    clonedList.add(newS); 
   } 
   Student student = clonedList.get( 1 ); 
   student.setName( "Jhon" ); 
   System.out.println( "Cloned list : " + clonedList); 
   System.out.println( "Original list : " + list); 
  }  } 

输出:

 Cloned list : [Student [id= 100 , name=harish], Student [id= 101 , name=Jhon]]  Original list : [Student [id= 100 , name=harish], Student [id= 101 , name=Jayaram]] 

5.结论

在本文中,您了解了如何克隆ArrayList以及有关ArrayList的浅表复制和深表复制的示例。

GitHub代码1
GitHub代码2
GitHub代码3

翻译自: https://www.javacodegeeks.com/2020/04/arraylist-clone-arraylist-deep-copy-and-shallow-copy.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值