List Problem

i have a problem adding a custom object i created into a list

my object is defined as

privatestaticclass PacketType{

int type;

int SeqNum;

int length;

char[] data =newchar[64];

}

I create an instance of this object, modify the insides, and add it to the list. This is done in a loop and the same object is modified and added to the list several times

However once i itirate through the list, only the last object is shown, and is shown as many time as the object was added

WHAT I THINK THE PROBLEM IS

When i add the object into the list, i actually add the address to this object. Since the object is constantly being "Updated" the most recent addition applies to all additions.

Is a way around this? Making every time i add the object to the list, it creates its own object, and isnt changed

Perhaps theres a keyword for the object defenition that insures unique adresses? im stuck

[1323 byte] By [samhebeisena] at [2007-11-26 20:39:27]
# 1

> I create an instance of this object, modify the insides, and add it to the list.

No, you don't :-) Variables in Java do not contain objects. They can contain an object reference (whose value is either null or a pointer to an object).

> Is a way around this? Making every time i add the object to the list, it creates its own object, and isnt changed

No, not as such. What you ought to do is just create a new, separate object in each loop iteration, and add the new reference to the list.

This is nothing specific to lists or collections; it's just the way reference variables and objects are defined in Java.

Lokoa at 2007-7-10 1:56:56 > top of Java-index,Java Essentials,Java Programming...
# 2

> Is a way around this?

Yes.

The solution is simple. Don't do this:

List list = new ArrayList();

PacketType packet = new PacketType ();

while (...) {

//set packet properties

list.add(packet);

}

... because in the end, list will contain may references to the same

PacketType object (think: new PacketType is only called once!)

Instead, simply do this:

List list = new ArrayList();

while (...) {

PacketType packet = new PacketType ();

//set packet properties

list.add(packet);

}

DrLaszloJamfa at 2007-7-10 1:56:56 > top of Java-index,Java Essentials,Java Programming...
# 3

thanks! Now im getting individual references, i know this because the different variables of my custom object reflect the changes, however, this is not the case for my

char[] data = new char[64];

variable. Im certain its a memory referencing problem. Is there a way to tell the object that it should make a new char everytime another object is created?

samhebeisena at 2007-7-10 1:56:56 > top of Java-index,Java Essentials,Java Programming...
# 4
Perhaps i should keep it as a string instead of an array of characters?
samhebeisena at 2007-7-10 1:56:56 > top of Java-index,Java Essentials,Java Programming...
# 5

You can do something like the following:

class PacketType{

private int type;

private int seqNum;

private int length;

private char[] data;

public PacketType(int type, int seqNum, int length, char[] data) {

this.type = type;

this.seqNum = seqNum;

this.length = this.length;

this.data = data.clone();

}

}

If you are using an old (<5) version of Java, that last line should be written:

this.data = (char[]) data.clone();

DrLaszloJamfa at 2007-7-10 1:56:57 > top of Java-index,Java Essentials,Java Programming...
# 6
If you're coding correctly then the choice String versus char[] should be onebased on principles, not "it's the one that I could get to work".
DrLaszloJamfa at 2007-7-10 1:56:57 > top of Java-index,Java Essentials,Java Programming...