Java Collections copy()方法及实例

Java Collections 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<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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程