當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Collections copy()用法及代碼示例


java.util.Collections類的copy()方法用於將所有元素從一個列表複製到另一個列表。

操作後,目標列表中每個複製元素的索引將與源列表中其索引相同。目標列表必須至少與源列表一樣長。如果更長,則目標列表中的其餘元素不受影響。

此方法以線性時間運行。


用法:

public static void copy(List dest, List src)

參數:此方法將以下參數作為參數

  • dest –目的地列表。
  • src –源列表。

異常:如果目標列表太小而無法包含整個源List,則此方法將引發IndexOutOfBoundsException。

以下示例說明了checkedSortedSet()方法

示例1:

// Java program to demonstrate 
// copy() method 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] args) 
        throws Exception 
    { 
        try { 
  
            // creating object of Source list and destination List 
            List<String> srclst = new ArrayList<String>(3); 
            List<String> destlst = new ArrayList<String>(3); 
  
            // Adding element to srclst 
            srclst.add("Ram"); 
            srclst.add("Gopal"); 
            srclst.add("Verma"); 
  
            // Adding element to destlst 
            destlst.add("1"); 
            destlst.add("2"); 
            destlst.add("3"); 
  
            // printing the srclst 
            System.out.println("Value of source list: " + srclst); 
  
            // printing the destlst 
            System.out.println("Value of destination list: " + destlst); 
  
            System.out.println("\nAfter copying:\n"); 
  
            // copy element into destlst 
            Collections.copy(destlst, srclst); 
  
            // printing the srclst 
            System.out.println("Value of source list: " + srclst); 
  
            // printing the destlst 
            System.out.println("Value of destination list: " + destlst); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [1, 2, 3]

After copying:

Value of source list: [Ram, Gopal, Verma]
Value of destination list: [Ram, Gopal, Verma]

示例2:對於IndexOutOfBoundsException

// Java program to demonstrate 
// copy() method 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // creating object of Source list and destination List 
            List<String> srclst = new ArrayList<String>(3); 
            List<String> destlst = new ArrayList<String>(2); 
  
            // Adding element to srclst 
            srclst.add("Ram"); 
            srclst.add("Gopal"); 
            srclst.add("Verma"); 
  
            // Adding element to destlst 
            destlst.add("1"); 
            destlst.add("2"); 
  
            // printing the srclst 
            System.out.println("Value of source list: " + srclst); 
  
            // printing the destlst 
            System.out.println("Value of destination list: " + destlst); 
  
            System.out.println("\nAfter copying:\n"); 
  
            // copy element into destlst 
            Collections.copy(destlst, srclst); 
  
            // printing the srclst 
            System.out.println("Value of source list: " + srclst); 
  
            // printing the destlst 
            System.out.println("Value of destination list: " + destlst); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [1, 2]

After copying:

Exception thrown : 
java.lang.IndexOutOfBoundsException:
 Source does not fit in dest


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections copy() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。