📜  在Java中将一个 ArrayList 的元素复制到另一个 ArrayList

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

在Java中将一个 ArrayList 的元素复制到另一个 ArrayList

它是List接口的实现类。它允许重复对象/元素并维护插入顺序。您可以通过它的索引获取存在于 ArrayList 中的元素,现在您需要将其传递给 Getting (index) 方法。您可以使用 add() 方法将元素添加到 ArrayList 中。

ArrayList 初始化的语法:

ArrayList gfg=new ArrayList<>();

将一个 ArrayList 的元素复制到另一个 ArrayList

首先有两种方法,您实际上只需要将一个 ArrayList 的引用传递给另一个,在这种情况下,如果您更改了一个 ArrayList 值或元素,那么您可以在其他 ArrayList 中看到相同的更改。第二种方法是您将创建实际的重复项,这意味着如果您在一个 ArrayList 元素中进行更改,则它不会反映在另一个元素中。

方法 1:使用赋值运算符(=)

在这种方法中,我们将简单地将第一个 ArrayList 引用分配给第二个,但这里有一个重要方面需要注意,我们没有创建新对象,我们只是将第二个 ArrayList 指向第一个。因此,如果您对第一个 ArrayList 进行更改,它也会反映在第二个 ArrayList 中,因为您使用的是相同的对象。我们还可以更改一个 ArrayList 的一个值,并且可以在另一个值中查找相同的值,无论它是否更改。

句法 :

ArrayList gfg=new ArrayList<>();
ArrayList gfg2=gfg;

下面是上述问题陈述的实现。

Java
// Java Program for copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        // Assigning the first reference to second
        ArrayList gfg2 = gfg;
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list and you can
        // see the same change in the first Arraylist
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}


Java
// Java Program for  copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        // passing in the constructor
        ArrayList gfg2 = new ArrayList<>(gfg);
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list and you can
        // here we will not see the same change in the first
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}


Java
// Java Program for  copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        ArrayList gfg2 = new ArrayList<>();
  
        // adding element to the second ArrayList
        // by iterating over one by one
        for (Integer value : gfg) {
            gfg2.add(value);
        }
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
  
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list
        // here we will not see the same change in the first
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}


输出
-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =23
third element of second list =23

方法二:传入构造函数

在这种方法中,我们将简单地在第二个 ArrayList 的构造函数中传递第一个 ArrayList。通过使用这种方法,如果我们更改一个 ArrayList 元素/值,它不会影响另一个元素/值,因此这就是我们实际创建重复项的方法。我们还可以更改一个 ArrayList 的一个值,并且可以在另一个值中查找相同的值,无论它是否更改。

句法 :

ArrayList gfg=new ArrayList<>();
ArrayList gfg2=new ArrayList<>(gfg);

下面是上述问题陈述的实现:

Java

// Java Program for  copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        // passing in the constructor
        ArrayList gfg2 = new ArrayList<>(gfg);
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list and you can
        // here we will not see the same change in the first
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}
输出
-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =22
third element of second list =23

方法三:使用add()方法一一添加

在这种方法中,我们将遍历第一个 ArrayList 的每个元素并将该元素添加到第二个 ArrayList 中。在这里,如果您更改第一个 ArrayList 元素,则不会更改第二个 ArrayList 的元素。我们还可以更改一个 ArrayList 的一个值,并且可以在另一个值中查找相同的值,无论它是否更改。

句法 :

ArrayList gfg=new ArrayList<>();
ArrayList gfg2=new ArrayList<>();
for(Integer val: gfg){
gfg2.add(val);
}

下面是上述问题陈述的实现:

Java

// Java Program for  copying one ArrayList to another
  
import java.io.*;
import java.util.ArrayList;
  
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList gfg = new ArrayList<>();
  
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
  
        ArrayList gfg2 = new ArrayList<>();
  
        // adding element to the second ArrayList
        // by iterating over one by one
        for (Integer value : gfg) {
            gfg2.add(value);
        }
  
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
  
        for (Integer value : gfg2) {
            System.out.println(value);
        }
  
        // here we changed the third element to 23
        // we changed in second list
        // here we will not see the same change in the first
        gfg2.set(2, 23);
  
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}
输出
-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =22
third element of second list =23