I have a program containing a Linked List data structure to store custom TCP packet.
Test program reads in a packet, adds it to the Linked List queue, and immediately prints the packet value (a timestamp value) for confirmation. This process is repeated 10 times in a while loop, so there are 10 packets in the Linked List queue.
Problem comes with a check on the queue values after all packets have been read in. The problem being that only the value of the last packet read in, is shown, despite the knowledge that there are 9 other packets with different values (supposedly) in the queue.
If someone could help me understand this seemingly black magic I would be most grateful.
public void readPackets() throws IOException {
int counter = 1;
while(counter < 10){
packet = con.fillWIMPacket(packet);
packetQueue.add(packet);
System.out.println("Packet " + counter + " added to Queue");
System.out.println("Packet " + counter + " " + packet.toString());
counter++;
}
}
public void printPacketValues(){
System.out.println("Packet Queue size is " + packetQueue.size());
for(int i = 0; i < packetQueue.size(); i++){
System.out.println("Packet " + i + ": " + packetQueue.get(i));
}
}
I should clarify that the println() methods of the first readPackets() is showing data in the correct order. However, the println() of printPacketValues() simply shows the value of the last packet 10 times..
1 Answer