LinkedList addLast函数替换列表中的其他值

我目前正在实现用于发送数据包的队列。但是,我遇到了一个问题,当我在LinkedList上使用addLast函数时,它会将列表中的每个对替换为要添加的对。

队列:

Pair

queue = new LinkedList<>(); 已从javafx.util.Pair导入;

队列初始化:

    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 。也可以使用此方法发送较小的数据包,但是对于较小的数据包似乎可以正常工作。

运行代码后,将显示以下结果:

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

似乎每次将某些内容添加到队列中时,队列中的所有其他值都设置为相同的值。 如果有人知道其原因,我将不胜感激任何指示。

方法printByteBufferAsBytes:

ctrl+insert
ppppp12345 回答:LinkedList addLast函数替换列表中的其他值

在方法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(在Java中)上addLast()的实现如下:

 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中有https://centos.pkgs.org/7/postgresql-11-x86_64的实现,我建议使用它代替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()。

本文链接:https://www.f2er.com/2443616.html

大家都在问