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
相關用法
- Java Collections min()用法及代碼示例
- Java Collections max()用法及代碼示例
- Java Collections singletonList()用法及代碼示例
- Java Collections synchronizedSortedMap()用法及代碼示例
- Java Collections indexOfSubList()用法及代碼示例
- Java Collections unmodifiableCollection()用法及代碼示例
- Java Collections enumeration()用法及代碼示例
- Java Collections synchronizedSortedSet()用法及代碼示例
- Java Collections fill()用法及代碼示例
- Java Collections addAll()用法及代碼示例
- Java Collections replaceAll()用法及代碼示例
- Java Collections swap()用法及代碼示例
- Java Collections synchronizedCollection()用法及代碼示例
- Java Collections list()用法及代碼示例
- Java Collections synchronizedSet()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections copy() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。