In Java, while using collections, we come across various scenarios to copy one list’s elements into the other with a precaution that the index of elements must not be affected while copying. For such type of scenarios, java.util.Collections package provides us with a function copy() that copies elements present in one list into the other list, such that elements have the same index in the new list as well. While using this function, the destination list’s size must be greater than the size of the source list. This time complexity of this function is linear.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Following is a syntax of copy() in java with parameters:
Syntax:
public static void copy(List dest, List src)
Arguments:
Above function throws below exception:
Given below are the examples mentioned below:
To illustrate the occurrence of IndexOutOfBoundsException when the size of the destination list is less than the source list.
Code:
import java.util.*; public class Demo { public static void main(String[] args) { List<String> mysrclist = new ArrayList<String>(6); List<String> mydestlist = new ArrayList<String>(11); mysrclist.add("Lets practice"); mysrclist.add("Java"); mysrclist.add("with US"); mysrclist.add("EDUCBA"); mysrclist.add("is a great"); mysrclist.add("Learnig Platform"); Collections.copy(mydestlist, mysrclist); System.out.println("Elements of source list: "+mysrclist); System.out.println("Elements of destination list: "+mydestlist); } }
Output:
Since the destination list size is 0 and the size of the source list is 6, there is no place in the destination list at that particular index; thus, this below exception is thrown as an output.
Explanation:
Code:
import java.util.*; public class Demo { public static void main(String[] args) { List<String> mysrclist = new ArrayList<String>(6); List<String> mydestlist = new ArrayList<String>(11); mysrclist.add("Lets practice"); mysrclist.add("Java"); mysrclist.add("with US"); System.out.println("Elements of source list: "+mysrclist); mydestlist.add("EDUCBA"); mydestlist.add("is a great"); mydestlist.add("Learnig Platform"); System.out.println("Elements of destination list before copy() executes: "+mydestlist); Collections.copy(mydestlist, mysrclist); System.out.println("Elements of destination list after copy() executes: "+mydestlist); } }
Output:
Explanation:
In this example, we will see how the destination list elements get overwritten when elements of the source list get copied into it.
Code:
import java.util.*; public class Demo { public static void main(String[] args) { //Create lists for source and destination List<String> mysrclist = new ArrayList<String>(6); List<String> mydestlist = new ArrayList<String>(11); //Populate two source and destination lists mysrclist.add("DIgital Learning"); mysrclist.add("for Java"); mysrclist.add("is great"); System.out.println("Elements of source list: "+mysrclist); mydestlist.add("Book Learning"); mydestlist.add("is"); mydestlist.add("a great"); mydestlist.add("Learnig Platform"); mydestlist.add("For All"); System.out.println("Elements of destination list before copying : "+mydestlist); // copy into destination list Collections.copy(mydestlist, mysrclist); System.out.println("Elements of destination list after copying : "+mydestlist); } }
Output:
Explanation:
The collections package provides us with a public static method that can be used to copy one source list’s elements into the destination list at the same indexes. This method works only in the destination list, which is greater than the source list, and this operation has linear time complexity.
The above is the detailed content of copy() in Java. For more information, please follow other related articles on the PHP Chinese website!