Copy Elements of One Java Vector to Another Vector in Java
Last Updated :
15 Nov, 2021
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also.
Syntax :
Vector<Integer> gfg=new Vector<>();
Ways To copy elements of one vector to another:
- Passing in the constructor
- Adding one by one using add() method
Method 1: Passing in the constructor
- In this approach, we will simply pass the one Vector into the other Vector's constructor.
- By using this approach if we change in first vector values then it will not change the values of the second vector.
- This is the easiest way of duplicating the vector values.
In the below program we will,
- First, create one Vector of integers and add elements to it using add() method.
- After that, we will pass the first vector into the constructor of the second.
- Now we will change one value vector and will check in another vector also to verify whether changing one vector value does not affect another vector.
Java
// Java Program for copying one Vector to another
// by passing in the constructor
import java.io.*;
import java.util.Vector;
class GFG {
public static void main (String[] args) {
// creation of Vector of Integers
Vector<Integer> gfg=new Vector<>();
// adding elements to first Vector
gfg.add(11);
gfg.add(22);
gfg.add(24);
gfg.add(39);
// passing in the constructor
Vector<Integer> gfg2=new Vector<>(gfg);
//Iterating over second Vector
System.out.println("-----Iterating over the second Vector----");
for(Integer value: gfg2){
System.out.println(value);
}
// here we changed the third element to 23
// we changed in the second vector and you can
// here we will not see the same change in the first
gfg2.set(2,23);
System.out.println("third element of first vector ="+gfg.get(2));
System.out.println("third element of second vector ="+gfg2.get(2));
}
}
Output-----Iterating over the second Vector----
11
22
24
39
third element of first vector =24
third element of second vector =23
Method 2: Adding one by one using add() method
- In this approach, we will iterate over each element of Vector and add that element in the second Vector.
- Here if you change the first Vector element then it will not change the elements of the second Vector.
- It is not the best approach but it's a simple iteration process.
In the below program we will first create one Vector of integers and add elements to it after that we will iterate on that vector and add the element at each iteration to the other vector.
Java
// Java Program for copying one Vector to another
// by adding elements one by one using add() method
import java.io.*;
import java.util.Vector;
class GFG {
public static void main (String[] args) {
// creation of Vector of Integers
Vector<Integer> gfg=new Vector<>();
// adding elements to first Vector
gfg.add(50);
gfg.add(24);
gfg.add(95);
gfg.add(31);
Vector<Integer> gfg2=new Vector<>();
// adding element to the second Vector
// by iterating over one by one
for(Integer value: gfg){
gfg2.add(value);
}
// Iterating over second Vector
System.out.println("-----Iterating over the second Vector----");
for(Integer value :gfg2)
{
System.out.println(value);
}
// here we changed the third element to 23
// we changed in second Vector
// here we will not see the same change in the first
gfg2.set(2,23);
System.out.println("third element of first Vector ="+gfg.get(2));
System.out.println("third element of second Vector ="+gfg2.get(2));
}
}
Output-----Iterating over the second Vector----
50
24
95
31
third element of first Vector =95
third element of second Vector =23
Similar Reads
Java Program to Copy Elements of ArrayList to Vector Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collectio
4 min read
Copy Elements of Vector to Java ArrayList Since Vector class and ArrayList class both are part of Java Collections, ie Collection framework, so both of these classes can use methods available to the Collection framework. Copy() method is one of the methods of Collection Interface which is used to copy one list to another list, here list can
3 min read
How to Copy Elements from One PriorityQueue to Another in Java? In Java, a priority queue is a data structure that allows the users to store data based on their priority. In this article, we will learn how to copy elements from one priority queue to another in Java. Example Input: PriorityQueue original ={1,2,3,4,5}Output: PriorityQueue Copy ={1,2,3,4,5}Copy ele
2 min read
Reverse Order of All Elements of Java Vector Vector class Implements a dynamic array means it can shrink and expand its size as required just likely having the same operations like that in the arrays. Don't confuse it with ArrayList as there is a thin line between vector and ArrayList, where the vector is synchronized rest the insertion order
4 min read
Iterate Over Vector Elements in Java Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. We can iterate over vector by the following ways: Simple for-loopEnhanced for-loopIteratorsEnumeration interface Method 1: Simple for-loop The idea is
4 min read