Java 将字节数组插入 List<Byte> 的最简洁方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3532810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Most concise way to insert array of bytes into List<Byte>?
提问by fred basset
In some code I'm creating a List of Bytes, and want to insert an array of bytes into the list as I am building it. What is the cleanest way of doing this? See code below - thanks.
在某些代码中,我正在创建一个字节列表,并希望在构建它时将一个字节数组插入到列表中。这样做最干净的方法是什么?请参阅下面的代码 - 谢谢。
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);
}
}
采纳答案by polygenelubricants
IFyou have a Byte[] arr
-- an array of reference types -- you can use Arrays.asList(arr)
to get a List<Byte>
.
如果您有一个Byte[] arr
- 一个引用类型数组 - 您可以使用它Arrays.asList(arr)
来获取List<Byte>
.
IFyou have a byte[] arr
-- an array of primitives -- you can't use Arrays.asList(arr)
to get a List<Byte>
. Instead you'll get a one-element List<byte[]>
.
如果您有一个byte[] arr
- 原语数组 - 您不能Arrays.asList(arr)
用来获取List<Byte>
. 相反,你会得到一个单元素List<byte[]>
。
That is, while a
byte
can be boxed to aByte
, abyte[]
DOES NOTget autoboxed toByte[]
!
(also true for other primitives)
也就是说,虽然 a
byte
可以装箱到 aByte
,但 abyte[]
不会自动装箱到Byte[]
!
(对于其他原语也是如此)
So you have two choices:
所以你有两个选择:
- Just iterate over each
byte
inbyte[]
andadd
individually - Use libraries
- With Apache Commons Lang, you can convert
byte[]
toByte[]
- You can then
Arrays.asList
andaddAll
- You can then
- With Guavacan convert
byte[]
immediatelly toList<Byte>
- With Apache Commons Lang, you can convert
- 只需迭代每个
byte
inbyte[]
和add
单独 - 使用库
- 使用Apache Commons Lang,您可以转换
byte[]
为Byte[]
- 然后你可以
Arrays.asList
和addAll
- 然后你可以
- 用番石榴可以
byte[]
立即转换为List<Byte>
- 使用Apache Commons Lang,您可以转换
The first option looks like this:
第一个选项如下所示:
byte[] arr = ...;
for (byte b : arr) {
list.add(b);
}
The second option with Guava looks like this:
Guava 的第二个选项如下所示:
// requires Guava
byte[] arr = ...;
list.addAll(Bytes.asList(arr));
This uses Bytes.asList
from package com.google.common.primitives
. The package has other conversion utilities for other primitives too. The entire library is highly useful.
这使用Bytes.asList
从package com.google.common.primitives
. 该包也有用于其他原语的其他转换实用程序。整个库非常有用。
With Apache Commons Lang, you can use Byte[] toObject(byte[])
from ArrayUtils
:
与Apache Commons Lang中,你可以使用Byte[] toObject(byte[])
从ArrayUtils
:
// requires Apache Commons Lang
byte[] arr = ...;
list.addAll(Arrays.asList(ArrayUtils.toObject(arr)));
Related questions
相关问题
回答by Colin Hebert
There is the Arrays.asList()
method which exactly do that:
有一种Arrays.asList()
方法可以做到这一点:
Arrays.asList(getData());
So in your case :
所以在你的情况下:
list.addAll(Arrays.asList(getData()));
回答by instcode
This might not answer your question but it should be a good practice. If you are heavily manipulating an array of bytes, use the ByteBuffer instead. This class have many types of implementation which can give you the best performance & memory usage. One of them is the Direct ByteBuffer which some operations can run natively.
这可能无法回答您的问题,但它应该是一个很好的做法。如果您大量操作字节数组,请改用 ByteBuffer。这个类有很多类型的实现,可以给你最好的性能和内存使用。其中之一是 Direct ByteBuffer,某些操作可以在本地运行。
To put a byte or an array of bytes is as simple as eating a candy:
放置一个字节或一个字节数组就像吃糖果一样简单:
ByteBuffer.put(byte src);
ByteBuffer.put(byte[] src);
ByteBuffer.put(byte[] src, int offset, int length);
And the best thing is when you trying to get the bytes out: directly, no array copy's needed (you need to check the size though) :)
最好的事情是当您尝试取出字节时:直接,不需要数组副本(尽管您需要检查大小):)
byte[] data = ByteBuffer.array();
Hope you change your mind :)
希望你改变主意:)
回答by Jarekczek
Do you really need List<Byte>
? I also thought so, but changed my mind. ByteArrayOutputStream
was much better for me.
你真的需要List<Byte>
吗?我也是这么想的,但是改变了主意。ByteArrayOutputStream
对我来说好多了。
import java.io.ByteArrayOutputStream;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
// fill in buf or get a single byte b
bo.write(buf, 0, bytesRead);
bo.write(b);
byte[] resultArray = bo.toByteArray();