Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Copy Elements of One Java Vector to Another Vector in Java
Next article icon

Copy Elements of One Java Vector to Another Vector in Java

Last Updated : 15 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Passing in the constructor
  2. 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

Next Article
Copy Elements of One Java Vector to Another Vector in Java

R

rajatagrawal5
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Collections
  • Java-Vector
Practice Tags :
  • Java
  • Java-Collections

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences