Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2015
    Posts
    3
    Rep Power
    0

    Default How to copy the Vector / Object and change the Copy without changing Original

    Hi All, I wanted to know whats the best way to deep copy a vector and manipulate it without impacting Original

    I used clone / collection copy / and do an add on each item as below.. but for all the ways, somehow original also seems to be disturbed.
    Can someone help?

    1) collections.copy(vdest,vsrc)

    2) for(int k = 0; k < vsrc.size(); k++){
    a = vsrc.elementAt(k);
    vdest.add(a);
    }


    3)Vector v2 = (Vector) v1.clone();

    4)Vector<Obj> v2 = new Vector<Obj>(v1);

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    Unless you have overridden the clone() method, none of the above create a deep copy of the original collection, i.e. a shallow copy is created, where only the top level references are copied to the new collection.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    Jun 2015
    Posts
    3
    Rep Power
    0

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    Thanks, Will it be possible if i Copy the objects of Vector into an Array using java.util.Vector.copyInto() ?

    All i wanted is to change one Object in the vector temporarily and based on the change , set a Flag. But i do not want the changed Object into the Vector. Sorry if its very basic but still struggling to figure this out

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    you can very well use copyInto method.it will not affect the vector.
    Ramya:cool:

  5. #5
    Join Date
    Jun 2015
    Posts
    3
    Rep Power
    0

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    Seems like it changing.. Not sure am missing something basic

    This is draft version of it.. I copied into an array and looping through array and changed one of the Object by paasing into a method. But the vector is having the changed object

    v1.copyInto(anArray);

    for (Obj k : anArray) {
    ptyaddress = (obj) k;
    crntAddr= standardize(ptyaddress.getAddr());
    }

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    post your code if possible.
    Ramya:cool:

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    see the below sample code
    Java Code:
    import java.util.Vector;
     
    public class Test {
     
      public static void main(String[] args) {
        //create a Vector object
        Vector v = new Vector();
       
        //Add elements to Vector
        v.add("1");
        v.add("2");
        v.add("3");
        v.add("4"); 
        v.add("5");
       
        //declare an array to hold elements of Vector
        Object[] objArray = new Object[5];
       
        v.copyInto(objArray);
       
    
            //display contents of Object array
            System.out.println("array contents now after copying");
            for(int index=0; index < objArray.length ; index++)
              System.out.println(objArray[index]);
          
    	      System.out.println("Now iam gng to change array content ");
    		  objArray[1] = 2000;
    		  System.out.println("array contents  after changing");
              for(int index=0; index < objArray.length ; index++)
              System.out.println(objArray[index]);
          
    		  System.out.println("Now iam gng to display vector "+ v);
    		  
        }
        }
    Ramya:cool:

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    I don't get this line of answers. copyInto() also does a shallow copy.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to copy the Vector / Object and change the Copy without changing Original

    Yep, the proposed answer doesn't make any sense at all (and Strings are immuatble anyway). The OP does not only want a copy of a top level collection, but also a copy of the collections that are elements of the top level container (or any other mutable object type). The ultimate is a real deep copy of it all ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Making deep copy of object arraylist
    By GoodThing007 in forum New To Java
    Replies: 25
    Last Post: 11-24-2013, 12:22 PM
  2. Replies: 7
    Last Post: 10-09-2013, 08:48 PM
  3. Copy Constructor with Shallow Copy
    By Wnt2bsleepin in forum New To Java
    Replies: 1
    Last Post: 04-11-2012, 12:42 AM
  4. Object copy
    By log12 in forum New To Java
    Replies: 4
    Last Post: 04-03-2011, 09:54 PM
  5. Object copy
    By Oktam in forum New To Java
    Replies: 6
    Last Post: 07-21-2010, 08:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •