Knowledge Walls
John Peter
Pune, Maharashtra, India
Copy method to copy collections list in Java
1983 Views
CollectionsCopyMethodExample
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsCopyMethodExample {
    public static void main(String args[]){
        List<String> dataList = new ArrayList<String>();
            dataList.add("Ball");
            dataList.add("Apple");
            dataList.add("Dog");
            dataList.add("Cat");
            
        List<String> dataList2 = new ArrayList<String>();
            dataList2.add("Eagle");
            dataList2.add("Gun");
        
        System.out.println("Before");
        System.out.println(dataList);
        System.out.println(dataList2);
        
        //Copying dataList2 items to dataList
        //Copying item from dataList2 to dataList index by index.
        //If copying index is not in dataList then throws ArraysIndexOutOfBoundsException
        Collections.copy(dataList, dataList2);
        
        System.out.println("After");
        System.out.println(dataList);
        System.out.println(dataList2);
    }
}
Output 
Previous Topics
Previous lessons of current book.
Computer software engineer articles of One day One Thing to Know
Computer software engineer articles of One day One Thing to Know
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details