-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.2.0
-
generic
-
generic
Name: vi73552 Date: 04/11/99
Compile the following:
import java.awt.*;
import java.util.*;
public class test
{
class testclass
{
String st1;
String st2;
}
Vector master_v1, v1;
public test()
{
master_v1 = new Vector();
v1 = new Vector();
testclass tc1 = new testclass();
testclass tc2 = new testclass();
testclass tc3 = new testclass();
tc1.st1 = "tc1st1";
tc1.st2 = "tc1st2";
tc2.st1 = "tc2st1";
tc2.st2 = "tc2st2";
tc3.st1 = "tc3st1";
tc3.st2 = "tc3st2";
master_v1.addElement((Object)tc1);
master_v1.addElement((Object)tc2);
master_v1.addElement((Object)tc3);
}
public void exec()
{
for (int z = 0; z < 2; z++)
{
v1 = (Vector)master_v1.clone();
for(int i=0; i < v1.size();i++)
{
System.out.println("master tc:"+i);
System.out.println("st1:"+i+" = "+((testclass)v1.elementAt(i)).st1);
System.out.println("st2:"+i+" = "+((testclass)v1.elementAt(i)).st2);
}
testclass temp = (testclass)v1.remove(1);
temp.st2 = "this is new!!";
v1.addElement((Object)temp);
for(int i=0; i < v1.size();i++)
{
System.out.println("tc:"+i);
System.out.println("st1:"+i+" = "+((testclass)v1.elementAt(i)).st1);
System.out.println("st2:"+i+" = "+((testclass)v1.elementAt(i)).st2);
}
}
v1.clear();
}
public static void main(String argv[])
{
test t1 = new test();
t1.exec();
}
}
Notice in the output that the second time thru for "master tc:tc2.st2" output
won't be "tc2st2", but "this is new!!". Per the JDK API docs for Vector.clone():
Returns a clone of this vector. The copy will contain a reference to a clone of the internal data
array, not a reference to the original internal data array of this Vector object.
This is not happening, instead, this acts more like ArrayList.clone() which states:
Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
I'm trying to use vectors as a substitute for dynamically linked lists but if I can't
clone them off a master vector list, its a pretty useless class.
(Review ID: 56549)
======================================================================