How do I copy one vector to another vector in Java without updating the original vector?

cauchy

I am copying a vector to another vector of same type.And modifying the copy vector but the original vector is also getting update I don't understand why?

Vector<allocated>finished_copy=new Vector<allocated>();
finished_copy=finished;

I am printing the value of original vector after and before modification

for(int k=0;k<finished.size();k++) {
        System.out.print(finished.elementAt(k).output);
        System.out.print(finished.elementAt(k).step);
        System.out.println();
}//some modification on finished_copy

And printing the original but both are different

Please help me in this

JB Nizet

You're not doing any copy. All you're doing is assigning the same Vector to another variable:

Before:

finished ------> [a, b, c, d]

After:

finished ------> [a, b, c, d]
                 ^
                 |
finished_copy ---/

Here's how you would copy all the elements of a Vector into another one:

Vector<Allocated> finishedCopy = new Vector<>(finished);

or

Vector<Allocated> finishedCopy = new Vector<>();
finishedCopy.addAll(finished);

This, however, will create two different vectors containing references to the same Allocated instances. If what you want is a copy of those objects, then you need to create explicit copies. Without doing a copy, changing the state of an object in the first list will also change its state in the second one, since both lists contain references to the same ojects.

Note that:

  • classes should start with an uppercase letter
  • variables should not contain underscares, but be camelCased
  • Vector should not be used anymore. ArrayList should be, since Java 2. We're at Java 8.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fastest way to copy one vector into another conditionally

copy one vector to another, omitting NA values

Copy a vector to another vector in reverse order

How to copy references of elements of one vector to another vector?

c++, how do I copy a vector into a vector of vectors?

CGAL: How do I rotate one 3D vector onto another?

How would one store the result of filtering a vector inside another vector without copying

how to copy a vector in std::vector<Eigen::vectorXd> to another vector in another std::vector<Eigen::vectorXd>

How do I add a vector to another holding the first vector constant?

How to move one vector into another vector without copying

How can I copy a vector to the end of another vector?

How do I copy the strings in a vector<pair<string, int>> to vector<string>?

how do you copy the contents of reference to a stl vector to another stl vector?

Filter/copy a vector based on elements in another vector

Copy items from one vector to another vector

How can I copy one vector to another through different classes

Copy vector element of a vector to another vector

LibGDX: how do I multiply vector with scalar without modifing original vector? (simple gravity system in Java game)

How to copy a vector to a subvector of another vector without segfaulting?

Appending one vector to another vector

How do I move a unique pointer from one vector to another vector of unique pointers?

how can i sort a 2d vector without change the original vector with insertion sort

How do I remove the elements of vector that occur in another vector in Rust?

How may I sort vectors for one vector by another vector?

How do I prevent one value from exceeding another value in a vector when minimizing it?

How do I add only the last element of one vector to the last element of another vector?

How do I copy a class with a std::vector as a member variable?

How to move an object from one vector to another, without destroying any?

How can I copy a vector mask from one document to another?