Java - dynamically create variable names

Associate
Joined
19 Jun 2006
Posts
162
Location
Swansea, Wales
Hi,

I am reading packets of a network and storing them via vectors.

Each packet has an ID number which is NOT unique to each packet but they are randomly spread through the traffic.

I want to read the ID number in each packet received and then create a vector with the name of that ID number. Or, if the vector already exists, pop the packet onto that vector.

E.g. (pseudo-code)

Packet.readID(packet);
Vector <idnumber> = new Vector();

Is this possible?

Any input appreciated.
 
OK i looked at maps today and made my code but its not exactly what I want because i have only a single vector for all my packets whereas i wanted a vector for each id.

I have...

Code:
if (callMap.containsKey(callID)) {
  packetStack.add(packet);
} else {
  callMap.put(callID, packetStack);
  packetStack.add(packet);
}

But would prefer something like...

Code:
if (callMap.containsKey(callID)) {
  packetStack1.add(packet);
} else { /* new callID so create unique vector to which i can write packets */
  callMap.put(callID, packetStack2);
  packetStack2.add(packet);
}
 
With the above code i get an error in Eclipse...

"The method add(Packet) is undefined for the type Object"

:(
 
Back
Top Bottom