📜  Java中的集合 copy() 方法和示例

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

Java中的集合 copy() 方法和示例

Java.util.Collections类的copy()方法用于将一个列表中的所有元素复制到另一个列表中。

操作后,目标列表中每个复制元素的索引将与其在源列表中的索引相同。目标列表必须至少与源列表一样长。如果它更长,则目标列表中的其余元素不受影响。

此方法以线性时间运行。

句法:

public static void copy(List dest, List src)

参数:此方法将以下参数作为参数

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

异常:如果目标列表太小而无法包含整个源列表,则此方法将引发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 srclst = new ArrayList(3);
            List destlst = new ArrayList(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 srclst = new ArrayList(3);
            List destlst = new ArrayList(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