【发布时间】:2010-08-20 16:21:23
【问题描述】:
在某些代码中,我正在创建一个字节列表,并希望在构建它时将一个字节数组插入到列表中。这样做最干净的方法是什么?请参阅下面的代码 - 谢谢。
public class ListInsert {
public static byte[] getData() {
return new byte[]{0x01, 0x02, 0x03};
}
public static void main(String[] args) {
final List<Byte> list = new ArrayList<Byte>();
list.add((byte)0xaa);
list.add(getData()); // I want to insert an array of bytes into the list here
list.add((byte)0x55);
}
}
【问题讨论】:
-
下面的答案很好,但我很好奇你打算用你的字节列表做什么......
-
这是我正在使用的设备,需要从串口发送某些消息包。
-
使用列表
是处理串行通信的一种非常低效的方法。您应该围绕字节数组编写自己的类似列表的包装器,或者使用 java.nio 中的字节缓冲区。