简体   繁体   English

Java-链​​表存储困难

[英]Java - Linked List Storage Difficulty

I have a program containing a Linked List data structure to store custom TCP packet. 我有一个包含链接列表数据结构的程序,用于存储自定义TCP数据包。

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. 此过程在while循环中重复10次,因此“链表”队列中有10个数据包。

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. 问题是在读完所有数据包之后检查队列的值。问题是,尽管知道有9个其他数据包具有不同的值(假定),但只显示了最后一个数据包的值。队列。

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. 我应该澄清一下,第一个readPackets()的println()方法以正确的顺序显示数据。 However, the println() of printPacketValues() simply shows the value of the last packet 10 times.. 但是,printPacketValues()的println()仅显示最后一个数据包的值10次。

while(counter < 10){
    packet = new PacketType(); //change here and try once.
    packet = con.fillWIMPacket(packet);
    packetQueue.add(packet); 
    System.out.println("Packet " + counter + " added to Queue");
    System.out.println("Packet " + counter + " " + packet.toString());
    counter++;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM