This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes deep copy of vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "deep copy of vector" Watch "deep copy of vector" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: deep copy of vector
 
Similar Threads
a simple counter 1 to 100
Comparable interface
cloning doubt
synchronizing a block of code
ARGH! Package problem while using *