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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:36:37  来源:igfitidea点击:

Most concise way to insert array of bytes into List<Byte>?

javalist

提问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 bytecan be boxed to a Byte, a byte[]DOES NOTget autoboxed to Byte[]!
(also true for other primitives)

也就是说,虽然 abyte可以装箱到 a Byte,但 abyte[]不会自动装箱到Byte[]
(对于其他原语也是如此)

So you have two choices:

所以你有两个选择:

  • Just iterate over each bytein byte[]and addindividually
  • Use libraries
    • With Apache Commons Lang, you can convert byte[]to Byte[]
      • You can thenArrays.asListand addAll
    • With Guavacan convert byte[]immediatelly to List<Byte>
  • 只需迭代每个byteinbyte[]add单独
  • 使用库
    • 使用Apache Commons Lang,您可以转换byte[]Byte[]
      • 然后你可以Arrays.asListaddAll
    • 番石榴可以byte[]立即转换为List<Byte>

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.asListfrom package com.google.common.primitives. The package has other conversion utilities for other primitives too. The entire library is highly useful.

这使用Bytes.asListpackage 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. ByteArrayOutputStreamwas 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();