| Author |
deep copy of vector
|
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
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
Joined: Jun 27, 2008
Posts: 63
|
|
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. */
|
 |
J. Nuno
Greenhorn
Joined: Jul 11, 2008
Posts: 13
|
|
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
|
 |
 |
|
|
subject: deep copy of vector
|
|
|