Java 集合 copy() 方法



描述

Java 集合 copy(List<? super T>, List<? extends T>) 方法用於將一個列表中的所有元素複製到另一個列表。

宣告

以下是 java.util.Collections.copy() 方法的宣告。

public static <T> void copy(List<? super T> dest,List<? extends T> src)

引數

  • dest − 這是目標列表。

  • src − 這是源列表。

返回值

異常

  • IndexOutOfBoundsException − 如果目標列表太小而無法容納整個源列表,則丟擲此異常。

  • UnsupportedOperationException − 如果目標列表的列表迭代器不支援 set 操作,則丟擲此異常。

複製整數列表示例

以下示例演示瞭如何使用 Java 集合 copy(List,List) 方法複製整數列表。我們建立了兩個包含一些整數的列表。使用 copy(List,List) 方法,我們將一個列表的內容複製到另一個列表,同時覆蓋現有內容。

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<Integer> srclst = new ArrayList<>();
      List<Integer> destlst = new ArrayList<>();

      // populate two lists
      srclst.add(1);
      srclst.add(2);
      srclst.add(3);

      destlst.add(4);
      destlst.add(5);
      destlst.add(6);
      destlst.add(7);

      // copy into dest list
      Collections.copy(destlst, srclst);            

      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Value of source list: [1, 2, 3]
Value of destination list: [1, 2, 3, 7]

複製字串列表示例

以下示例演示瞭如何使用 Java 集合 copy(List,List) 方法複製字串列表。我們建立了兩個包含一些字串的列表。使用 copy(List,List) 方法,我們將一個列表的內容複製到另一個列表,同時覆蓋現有內容。

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<String> srclst = new ArrayList<>();
      List<String> destlst = new ArrayList<>();

      // populate two lists
      srclst.add("A");
      srclst.add("B");
      srclst.add("C");

      destlst.add("D");
      destlst.add("E");
      destlst.add("F");
      destlst.add("G");

      // copy into dest list
      Collections.copy(destlst, srclst);            

      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Value of source list: [A, B, C]
Value of destination list: [A, B, C, G]

複製物件列表示例

以下示例演示瞭如何使用 Java 集合 copy(List,List) 方法複製 Student 物件列表。我們建立了兩個包含一些 Student 物件的列表。使用 copy(List,List) 方法,我們將一個列表的內容複製到另一個列表,同時覆蓋現有內容。

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<Student> srclst = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));
      List<Student> destlst = new ArrayList<>(Arrays.asList(new Student(4, "Jene"),
         new Student(5, "Julie"), new Student(6, "Trus")));

      // copy into dest list
      Collections.copy(destlst, srclst);            

      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Value of source list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Value of destination list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_collections.htm
廣告
© . All rights reserved.