How to Copy Vector Elements into ArrayList in Java


This tutorial will help us learn how to copy elements of Vector to ArrayList with copy method of Collections class in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Vector;

public class CopyElementsVectorToArrayList {

	public static void main(String[] args) {

		// Create vector object
		Vector vector = new Vector();

		// Add elements
		vector.add("A");
		vector.add("B");
		vector.add("C");
		vector.add("D");
		vector.add("E");
		
		// Create an ArrayList object that specifies that its elements are
		// elements in a vector
		ArrayList aList = new ArrayList(vector);

		System.out.println("aList:" + aList);
	}

}

Output:

aList:[A, B, C, D, E]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments