📜  如何在Java中将 ArrayList 克隆到另一个 ArrayList?

📅  最后修改于: 2022-05-13 01:54:20.794000             🧑  作者: Mango

如何在Java中将 ArrayList 克隆到另一个 ArrayList?

ArrayList 类的 clone() 方法用于将一个 ArrayList 克隆到Java中的另一个 ArrayList,因为它返回其调用方 ArrayList 的浅拷贝。

句法:

返回值:此函数返回 Object 实例的副本。

下面的程序说明了Java.util.ArrayList.clone() 方法:

例子:

Java
// Java program to clone an ArrayList to another ArrayList
  
import java.util.ArrayList;
public class GFG {
  
    public static void main(String a[])
    {
  
        // create ArrayList
        ArrayList ArrList1
            = new ArrayList();
  
        // Insert elements in ArrayList
        ArrList1.add("Mukul");
        ArrList1.add("Rahul");
        ArrList1.add("Suraj");
        ArrList1.add("Mayank");
  
        // print ArrayList1
        System.out.println("Original ArrayList = "
                           + ArrList1);
  
        // clone ArrayList
        ArrayList ArrList2
            = (ArrayList)ArrList1.clone();
  
        // print ArrayList2
        System.out.println("Clone ArrayList2 = "
                           + ArrList2);
    }
}


Java
// Java code to illustrate clone() method
  
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
  
    public static void main(String args[])
    {
  
        // Creating an empty ArrayList
        ArrayList list = new ArrayList();
  
        // Use add() method
        // to add elements in the list
        list.add(16);
        list.add(32);
        list.add(48);
  
        // Displaying the list
        System.out.println("First ArrayList: " + list);
  
        // Creating another linked list and copying
        // creates a shallow copy
        ArrayList sec_list
            = (ArrayList)list.clone();
  
        sec_list.add(64);
  
        // Displaying the list
        System.out.println("First ArrayList: " + list);
          
        // Displaying the other linked list
        System.out.println("Second ArrayList is: "
                           + sec_list);
    }
}


输出
Original ArrayList = [Mukul, Rahul, Suraj, Mayank]
Clone ArrayList2 = [Mukul, Rahul, Suraj, Mayank]

示例 2:

Java

// Java code to illustrate clone() method
  
import java.io.*;
import java.util.*;
  
public class ArrayListDemo {
  
    public static void main(String args[])
    {
  
        // Creating an empty ArrayList
        ArrayList list = new ArrayList();
  
        // Use add() method
        // to add elements in the list
        list.add(16);
        list.add(32);
        list.add(48);
  
        // Displaying the list
        System.out.println("First ArrayList: " + list);
  
        // Creating another linked list and copying
        // creates a shallow copy
        ArrayList sec_list
            = (ArrayList)list.clone();
  
        sec_list.add(64);
  
        // Displaying the list
        System.out.println("First ArrayList: " + list);
          
        // Displaying the other linked list
        System.out.println("Second ArrayList is: "
                           + sec_list);
    }
}

输出

First ArrayList: [16, 32, 48]
First ArrayList: [16, 32, 48]
Second ArrayList is: [16, 32, 48, 64]