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



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

i copying vector vector of same type.and modifying re-create vector original vector getting update don't understand why?

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

i printing value of original vector after , 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 original both different

please help me in this

you're not doing copy. you're doing assigning same vector variable:

before:

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

after:

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

here's how re-create elements of vector one:

vector<allocated> finishedcopy = new vector<>(finished);

or

vector<allocated> finishedcopy = new vector<>(); finishedcopy.addall(finished);

this, however, create 2 different vectors containing references same allocated instances. if want re-create of objects, need create explicit copies. without doing copy, changing state of object in first list alter state in sec one, since both lists contain references same ojects.

note that:

classes should start uppercase letter variables should not contain underscares, camelcased vector should not used anymore. arraylist should be, since java 2. we're @ java 8.

java vector copy

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -