How to copy all elements of an ArrayList to another ArrayList in Java

1 Answer

0 votes
package javaapplication1;

import java.util.ArrayList;
import java.util.Collections;

public class JavaApplication1 {

    public static void main(String[] args) {

        ArrayList arrList1 = new ArrayList();

        arrList1.add("Java");
        arrList1.add("C#");
        arrList1.add("C++");
        arrList1.add("PHP");

        ArrayList arrList2 = new ArrayList();

        arrList2.add("Iron Man");
        arrList2.add("Inception");
        arrList2.add("Skyline");
        arrList2.add("Tron");
        arrList2.add("Limitless");
        arrList2.add("Thor");

        System.out.println(arrList2);  
        
        // copy all elements of arrList1 to arrList2
        Collections.copy(arrList2, arrList1);

        System.out.println(arrList2);  
  }
}
  
/*
run:

[Iron Man, Inception, Skyline, Tron, Limitless, Thor]
[Java, C#, C++, PHP, Limitless, Thor]

*/

 



answered Sep 24, 2016 by avibootz

Related questions

1 answer 224 views
1 answer 148 views
1 answer 146 views
2 answers 220 views
2 answers 213 views
2 answers 268 views
...