当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。