Byte集合转数组的问题,在线等

JinuoBichang 2014-08-20 04:23:49
求助:

List<Byte> bList = new ArrayList<Byte>();
bList.add((byte)1);
bList.add((byte)2);
bList.add((byte)3);
bList.add((byte)4);

System.out.println("hello");
for (int i = 0; i < bList.size(); i++) {
System.out.println("--->>"+bList.get(i));
}

byte[] bArr = new byte[bList.size()];
//如何将bList赋给bArr,C#中bList.CopyTo(bArr),在Java中该怎么处理
...全文
368 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
JinuoBichang 2014-08-21
  • 打赏
  • 举报
回复
引用 6 楼 xiangnan129 的回复:

List<Byte> bList = new ArrayList<Byte>();
        bList.add((byte)1);
        bList.add((byte)2);
        bList.add((byte)3);
        bList.add((byte)4);
         
        System.out.println("hello");
        for (int i = 0; i < bList.size(); i++) {
            System.out.println("--->>"+bList.get(i));
        }

        Byte [] arrs = new Byte[bList.size()];
        //将集合bList转换成arrs数组。
        bList.toArray(arrs);
引用 6 楼 xiangnan129 的回复:

List<Byte> bList = new ArrayList<Byte>();
        bList.add((byte)1);
        bList.add((byte)2);
        bList.add((byte)3);
        bList.add((byte)4);
         
        System.out.println("hello");
        for (int i = 0; i < bList.size(); i++) {
            System.out.println("--->>"+bList.get(i));
        }

        Byte [] arrs = new Byte[bList.size()];
        //将集合bList转换成arrs数组。
        bList.toArray(arrs);
谢了,集合用Byte ,数组用byte.用遍历解决了.
scott_129 2014-08-20
  • 打赏
  • 举报
回复

List<Byte> bList = new ArrayList<Byte>();
        bList.add((byte)1);
        bList.add((byte)2);
        bList.add((byte)3);
        bList.add((byte)4);
         
        System.out.println("hello");
        for (int i = 0; i < bList.size(); i++) {
            System.out.println("--->>"+bList.get(i));
        }

        Byte [] arrs = new Byte[bList.size()];
        //将集合bList转换成arrs数组。
        bList.toArray(arrs);
vnvlyp 2014-08-20
  • 打赏
  • 举报
回复
Java没有相应的函数吧?4楼就是很好的代码啊,你调用那些函数内部实现也就是差不多的代码。 不过相对于4楼的代码可以稍微优化一下,调用get在使用LinkedList时候效率太低,这样写可能稍微好一点。
		byte[] bArr = new byte[bList.size()];
		int i = 0;
		for (byte b : bList) {
			bArr[i++] = b;
		}
shixitong 2014-08-20
  • 打赏
  • 举报
回复
引用 3 楼 JinuoBichang 的回复:
[quote=引用 1 楼 shixitong 的回复:]
        Byte[] bArr = new Byte[bList.size()];
        bArr=bList.toArray(new Byte[0]);
        System.out.println(Arrays.asList(bArr));
bArr 需要是byte[],不可以是Byte[].[/quote] 笨点的
        byte[] bArr = new byte[bList.size()];
        for(int i = 0; i < bList.size(); i++)
        {
        	bArr[i]=bList.get(i);
        }
JinuoBichang 2014-08-20
  • 打赏
  • 举报
回复
引用 1 楼 shixitong 的回复:
        Byte[] bArr = new Byte[bList.size()];
        bArr=bList.toArray(new Byte[0]);
        System.out.println(Arrays.asList(bArr));
bArr 需要是byte[],不可以是Byte[].
S117 2014-08-20
  • 打赏
  • 举报
回复

    Byte[] bArr = new Byte[bList.size()];
    bList.toArray(bArr); 
    System.out.println(Arrays.toString(bArr));
shixitong 2014-08-20
  • 打赏
  • 举报
回复
        Byte[] bArr = new Byte[bList.size()];
        bArr=bList.toArray(new Byte[0]);
        System.out.println(Arrays.asList(bArr));

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧