java - LinkedList addLast 函数替换列表中的其他值
问题描述
我目前正在实现一个发送数据包的队列。但是,我遇到了一个问题,当我在 LinkedList 上使用 addLast 函数时,它会将列表中的每个 Pair 替换为我添加到其中的 Pair。
队列:
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
}
推荐阅读
- python - 如何最小化 Matplotlib 中子图之间的空间?
- react-native - React Native:如何使用 Context Api 在 HeaderRight 自定义组件中显示屏幕状态变量更新
- mysql - mysql选择json
- javascript - 在 React 中基于路由匹配类的问题
- html - 具有非矢量缩放的 svg 路径上的一致投影大小
- vagrant - 当我在做 vagrant up 时,我遇到了这个关于无法解析主机的错误
- r - 使用 readtext 从 XML 中提取文本
- python - 如何使用 lambda 函数在多个 S3 存储桶上启用版本控制?
- node.js - 有没有办法在无服务器中接受像 pdf 这样的二进制数据类型?
- timescaledb - 我们可以在 AWS RDS 上为 postgres 安装 TimescaleDB 作为 postgres 扩展吗?