• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

deep copy of vector

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a distinct copy (deep copy) of a vector (for example).
This code returns a reference. Can someone point out how to accomplish this task. Thanks.



import java.util.Vector;



public class Copy{

Vector vOriginal;
Vector vCopy;

//***********************************************************************
public Copy(){

}

public static void main(String[] args) {
Copy c = new Copy();
c.go();
}

//***********************************************************************
public void go(){
vOriginal = new Vector();
StringBuffer sb = new StringBuffer("the brown fox jumped the fence. ");

vOriginal.add(sb);

vCopy = new Vector();
for(int i=0;i<vOriginal.size();i++){
vCopy.add(vOriginal.elementAt(i));
}

System.out.println("original: " + vOriginal.elementAt(0));
System.out.println("copy: " + vOriginal.elementAt(0));

sb.append(" The fox chased the rabbit.");

System.out.println("original: " + vOriginal.elementAt(0));
System.out.println("copy: " + vOriginal.elementAt(0));

}


}

/*

output:
original: the brown fox jumped the fence.
copy: the brown fox jumped the fence.
original: the brown fox jumped the fence. The fox chased the rabb
copy: the brown fox jumped the fence. The fox chased the rabbit.

*/
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry: error in previous code. Still the same issue.

import java.util.Vector;



public class Copy{

Vector vOriginal;
Vector vCopy;

//***********************************************************************
public Copy(){

}

public static void main(String[] args) {
Copy c = new Copy();
c.go();
}

//***********************************************************************
public void go(){
vOriginal = new Vector();
StringBuffer sb = new StringBuffer("the brown fox jumped the fence. ");

vOriginal.add(sb);

vCopy = new Vector();
for(int i=0;i<vOriginal.size();i++){
vCopy.add(vOriginal.elementAt(i));
}

System.out.println("original: " + vOriginal.elementAt(0));
System.out.println("copy: " + vCopy.elementAt(0));

sb.append(" The fox chased the rabbit.");

System.out.println("original: " + vOriginal.elementAt(0));
System.out.println("copy: " + vCopy.elementAt(0));

}


}

/*

output:
original: the brown fox jumped the fence.
copy: the brown fox jumped the fence.
original: the brown fox jumped the fence. The fox chased the rabb
copy: the brown fox jumped the fence. The fox chased the rabbit.

*/
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Your code doesn't work because of two reasons:
1) Your copying the vector but using the same elements it contains
2) Even if you'd copied the elements, you're updating the original StringBuffer

The solution could be:
Replace vCopy.add(vOriginal.elementAt(i)); with vCopy.add(new StringBuffer(vOriginal.elementAt(i).toString()));

and sb.append(" The fox chased the rabbit."); with ((StringBuffer) vCopy.get(0)).append(" The fox chased the rabbit.");

Regards
reply
    Bookmark Topic Watch Topic
  • New Topic