Java的:转换字节清单的字节数组 [英] Java : convert List of Bytes to array of bytes

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

问题描述

想解决什么应该是一个简单的问题。 GOT字节的名单,希望它在函数结束转换为字节数组。

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?

推荐答案

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

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

你可以做的是使用公地郎 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)

如果你可以用住字节[] 而不是字节[] - 离开这种方式

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

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

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