簡體   English   中英

LinkedList addLast function 替換列表中的其他值

[英]LinkedList addLast function replacing other values in list

我目前正在實現一個發送數據包的隊列。 但是,我遇到了一個問題,當我在 LinkedList 上使用 addLast function 時,它會將列表中的每一對替換為我添加到其中的對。

隊列:

private LinkedList<Pair<Integer, ByteBuffer>> queue;

Pair已從 javafx.util.Pair 導入;

隊列的初始化:

queue = new LinkedList<>();

方法:

    public synchronized void addToQueue(int bytes, ByteBuffer data) {
        Pair<Integer, ByteBuffer> local = new Pair(bytes, data);
        queue.addLast(new Pair(bytes, data));

        if(bytes>2){
            int i = 0;
            for(Pair<Integer,ByteBuffer> datas:queue ){
                System.out.println("\n Data in the "+i+ "th position in queue is: ");
                printByteBufferAsBytes(datas.getValue(), datas.getKey());
                i++;
            }
        }

    }

為了調試,每當發送數據包時,我一直在打印data 此方法也可用於發送較小的數據包,但它似乎適用於較小的數據包。

運行代碼后,將打印以下結果:

Data in the 0th position in queue is: 
1 5 40 -128 -58 0 0 42 111 34 -24 0 0 0 0 112 114 105 110 116 66 121 116 101 66 117 102 102 101 114 65 115 something was added to queue

 Data in the 0th position in queue is: 
2 5 40 -128 -58 17 0 115 -86 119 76 66 121 116 101 115 40 113 117 101 117 101 46 112 101 101 107 40 41 46 103 101 
 Data in the 1th position in queue is: 
2 5 40 -128 -58 17 0 115 -86 119 76 66 121 116 101 115 40 113 117 101 117 101 46 112 101 101 107 40 41 46 103 101 something was added to queue

 Data in the 0th position in queue is: 
2 5 40 -128 -58 38 0 -102 -46 -61 99 116 86 97 108 117 101 40 41 44 32 113 117 101 117 101 46 112 101 101 107 40 
 Data in the 1th position in queue is: 
2 5 40 -128 -58 38 0 -102 -46 -61 99 116 86 97 108 117 101 40 41 44 32 113 117 101 117 101 46 112 101 101 107 40 
 Data in the 2th position in queue is: 
2 5 40 -128 -58 38 0 -102 -46 -61 99 116 86 97 108 117 101 40 41 44 32 113 117 101 117 101 46 112 101 101 107 40 something was added to queue

 Data in the 0th position in queue is: 
3 5 40 -128 -58 59 0 109 60 120 12 11 41 46 103 101 116 75 101 121 40 41 41 101 117 101 46 112 101 101 107 40 
 Data in the 1th position in queue is: 
3 5 40 -128 -58 59 0 109 60 120 12 11 41 46 103 101 116 75 101 121 40 41 41 101 117 101 46 112 101 101 107 40 
 Data in the 2th position in queue is: 
3 5 40 -128 -58 59 0 109 60 120 12 11 41 46 103 101 116 75 101 121 40 41 41 101 117 101 46 112 101 101 107 40 
 Data in the 3th position in queue is: 
3 5 40 -128 -58 59 0 109 60 120 12 11 41 46 103 101 116 75 101 121 40 41 41 101 117 101 46 112 101 101 107 40

似乎每次將某些內容添加到隊列中時,隊列中的所有其他值都設置為相同的值。 如果有人知道原因,我將不勝感激任何指示。

方法 printByteBufferAsBytes:

    public void printByteBufferAsBytes(ByteBuffer bytes, int bytesLength) {
        for (int i = 0; i < bytesLength; i++) {
            System.out.print(Byte.toString(bytes.get(i)) + " ");
        }
    }

在方法addToQueue ,您需要將更新后的 ByteBuffer 的內容復制到一些新的字節數組中:

    public synchronized void addToQueue(int bytes, ByteBuffer data) {
        byte[] copy = Arrays.copyOf(data.array(), bytes);

        Pair<Integer, ByteBuffer> local = new Pair<>(bytes, ByteBuffer.wrap(copy));
        queue.addLast(local);
    // ... the rest of the method remains as is

   }

LinkedList 上 addLast() 的實現(Java 中)如下:

 public void addLast(AnyType item)
   {
      if( head == null)
         addFirst(item);
      else
      {
         Node<AnyType> tmp = head;
         while(tmp.next != null) tmp = tmp.next;

         tmp.next = new Node<AnyType>(item, null);
      }
   }

在 Java 中有一個Queue的實現,我建議使用它而不是 LinkedList。 此外,應該使用出隊和入隊方法來遵循術語。

Queue(Java)上的 enqueue() 和 dequeue() 方法定義如下:

public void enqueue(Item item) {
        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if (isEmpty()) first = last;
        else           oldlast.next = last;
        n++;
        assert check();
    }

public Item dequeue() {
        if (isEmpty()) throw new NoSuchElementException("Queue underflow");
        Item item = first.item;
        first = first.next;
        n--;
        if (isEmpty()) last = null;   // to avoid loitering
        assert check();
        return item;
    }

關於 LinkedLists 的一個小說明,方法 add() 等價於 addLast()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM