|
How to use deep copies (or something like it)?
Okay, for this situation I *think* I need a deep copy, but I'm not sure.
All variables are Strings. Object-type Person is an object class I defined with variables name, add1, add2, phone, and comments. What I'm trying to do is read those variables from a file, create a new Person object with those parameters, and then add this object to a Vector. The code:
Person guest;
for (......)
{
name = reader.readLine().trim();
add1 = reader.readLine().trim();
add2 = reader.readLine().trim();
phone = reader.readLine().trim();
comments = reader.readLine().trim();
guest = new Person(name, add1, add2, phone, comments);
guestsV.addElement(guest);
}
Some of you may already realize it's not working right. It appears that all "versions" of guest refer to the same memory address, so all I get is the same Person object in each Vector location. How do I get it to treat each object as something totally seperate? Do I need to use a deep copy? If so, how? Thanks for any help!
|