Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

deep copy of vector

 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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
  • 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.

*/
 
J. Nuno
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic