Java:将字节列表转换为字节数组 [英] Java : convert List of Bytes to array of bytes

查看:141
本文介绍了Java:将字节列表转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试解决应该是一个简单的问题。 。得到字节的名单,希望它在功能字节数组的结尾转换

Trying to solve what should be a simple problem. Got a list of Bytes, want to convert it at the end of a function to an array of bytes.

final List<Byte> pdu = new ArrayList<Byte>();
....
return pdu.toArray(new byte[pdu.size()]);;

编译器不喜欢我的 toArray语法。如何解决这一问题?

compiler doesn't like syntax on my toArray. How to fix this?

推荐答案

编译器不喜欢它,因为字节[] 不是 Byte []

The compiler doesn't like it, because byte[] isn't Byte[].

你可以做的是使用公郎 ArrayUtils.toPrimitive(wrapperCollection)

What you can do is use commons-lang's ArrayUtils.toPrimitive(wrapperCollection):

Byte[] bytes = pdu.toArray(new Byte[pdu.size()]);
return ArrayUtils.toPrimitive(bytes);

如果您不能使用公郎,只需通过数组循环和填充型另一个数组字节[] 与值(它们将被自动拆箱)

If you can't use commons-lang, simply loop through the array and fill another array of type byte[] with the values (they will be automatically unboxed)

如果你可以用<$ C $活c> Byte [] 而不是 byte [] - 就这样。

If you can live with Byte[] instead of byte[] - leave it that way.

这篇关于Java:将字节列表转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆