示例程序以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