从List< Byte>创建byte [] [英] Creating a byte[] from a List<Byte>

查看:138
本文介绍了从List< Byte>创建byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最有效的方法是什么?

推荐答案

byte[] byteArray = new byte[byteList.size()];
for (int index = 0; index < byteList.size(); index++) {
    byteArray[index] = byteList.get(index);
}

您可能不喜欢它,但这是创建Genuine™的唯一方法Array®的字节

You may not like it but that’s about the only way to create a Genuine™ Array® of byte.

正如评论中所指出的,还有其他方法。但是,这些方法都没有解决a)创建数组和b)分配每个元素。这个使用迭代器

As pointed out in the comments, there are other ways. However, none of those ways gets around a) creating an array and b) assigning each element. This one uses an iterator.

byte[] byteArray = new byte[byteList.size()];
int index = 0;
for (byte b : byteList) {
    byteArray[index++] = b;
}

这篇关于从List&lt; Byte&gt;创建byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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