Copy Elements of ArrayList to Java Vector



In order to copy elements of ArrayList to Java Vector, we use the Collections.copy() method. It is used to copy all elements of a collection into another.

Declaration −The java.util.Collections.copy() method is declared as follows −

public static <T> void copy(List<? super T> dest,List<? extends T> src)

where src is the source list object and dest is the destination list object

Let us see a program to copy elements of ArrayList to Java Vector −

Example

 Live Demo

import java.util.*;
public class Example {
   public static void main (String[] args) {
      List<String> zoo = new ArrayList<String>();
      zoo.add("Zebra");
      zoo.add("Lion");
      zoo.add("Tiger");
      Vector vect = new Vector();
      vect.add(1);
      vect.add(2);
      vect.add(3);
      Collections.copy(vect,zoo); // copying the ArrayList to the Vector
      System.out.println(vect);
   }
}

Output

[Zebra, Lion, Tiger]
Updated on: 2020-06-25T14:39:50+05:30

400 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements