Java >> Java チュートリアル >  >> Tag >> ArrayList

ArrayList clone() – ArrayList のディープ コピーとシャロー コピー

サンプル プログラムから ArrayList へのクローン メソッドの例。 Student Objects の ArrayList ディープ コピーとシャロー コピーの例。

1.はじめに

ArrayList clone() – ArrayList ディープ コピーとシャロー コピー . ArrayList clone() リストの浅いコピーを作成するためにメソッドが使用されます .新しいリストでは、オブジェクト参照のみがコピーされます。最初の ArrayList 内でオブジェクトの状態を変更すると、変更されたオブジェクトの状態は複製された ArrayList にも反映されます。

2. ArrayList クローン – 浅いコピーの例

文字列のリストを別のリストにコピーするプログラム例
クローン() .

0102030405060708091011121314151617181920212223242526272829303132 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);   } }

出力:

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

3. ArrayList クローン – カスタムの浅いコピーの例

カスタム オブジェクトを使用した ArrayList の浅いコピーへのサンプル プログラム。複製されたオブジェクトへの変更は、元のオブジェクトに反映されます。 Observer the output of this program.

01020304050607080910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 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 + "]" ;   } }

出力:

12 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);   } }

出力:

12 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

Java タグ