What is going on here?

Please help...this is driving me nuts!!!!

I'm trying to add two instances of an GameTable object to a Vector. However, when I try and add the second table (one GameTable has already been created and added), for some reason, when trying to add a second, the program modified the first object in the Vector and then adds the object.

The following output is displayed when this block is run for the first time (nowhere else in the code is variable that1Table referenced).

It's the two lines in bold which is really puzzling. Just by creating a new GameTable object I seem to have modifed the object the Vector is pointing to?

$$$$$$$$$$$$$$$$$$$$$$$$$$

$$$$$$ BEFORE ADDING $$$$$$$$

There is 1 in the Vector

Old Trafford

Obj at pos 0 in Vector is called Old Trafford

Obj at pos 0 in Vector is called Stamford Bridge

$$$$$$$$$$$$$$$$$$$$$$$$$$

$$$$$$ AFTER ADDING $$$$$$$$

There is 2 in the Vector

Stamford Bridge

Stamford Bridge

if (tables.size() == 1){

TextIO.putln("Obj at pos 0 in Vector is called " + ((GameTable)tables.get(0)).tableName);

GameTable that1Table =new GameTable(tabname);

TextIO.putln("Obj at pos 0 in Vector is called " + ((GameTable)tables.get(0)).tableName);

that1Table.start();

tables.add(that1Table);

}

Any idea?

[1556 byte] By [fdgfda] at [2007-10-3 10:22:41]
# 1

Full code to explain the output....

TextIO.putln("$$$$$$$$$$$$$$$$$$$$$$$$$$");

TextIO.putln("$$$$$$ BEFORE ADDING $$$$$$$$");

TextIO.putln("There is " + tables.size() + " in the Vector");

for (int i=0; i<tables.size(); i++) {

TextIO.putln(((GameTable)tables.get(i)).tableName);

}

if (tables.size() == 1) {

TextIO.putln("Obj at pos 0 in Vector is called " + ((GameTable)tables.get(0)).tableName);

GameTable that1Table = new GameTable(tabname);

TextIO.putln("Obj at pos 0 in Vector is called " + ((GameTable)tables.get(0)).tableName);

that1Table.start();

tables.add(that1Table);

} else {

GameTable that2Table = new GameTable(tabname);

that2Table.start();

tables.add(that2Table);

}

TextIO.putln("$$$$$$$$$$$$$$$$$$$$$$$$$$");

TextIO.putln("$$$$$$ AFTER ADDING $$$$$$$$");

TextIO.putln("There is " + tables.size() + " in the Vector");

>

fdgfda at 2007-7-15 5:44:19 > top of Java-index,Java Essentials,Java Programming...
# 2

Is the "tableName" field of GameTable static? It shouldn't be. Each table should have its own tableName. Your constructor is apparently setting tableName, affecting both instances of GameTable (the one already in the vector, as well as the new one). So, I'm guessing that tableName is erroneously static.

You should use ArrayList instead of Vector. If you need synchronization, use:

List myList = Collections.synchronizedList(new ArrayList());

doremifasollatidoa at 2007-7-15 5:44:19 > top of Java-index,Java Essentials,Java Programming...
# 3
HiThankyou very much!Yes it is static!! I still can't get my head around a static variable associated with two instances of an object but I'll keep trying!Why use ArrayList as opposed to Vector?Thanks
fdgfda at 2007-7-15 5:44:19 > top of Java-index,Java Essentials,Java Programming...