一个ArrayList里的元素全部是Byte对象.怎么才能把这个ArrayList转换成一个byte[](不是Byte[])

解决方案 »

  1.   

    package com.bob.exam;import java.util.*;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    class Untitled5 {
        public Untitled5() {
        }
    }
    class sub extends Untitled15 {
        public static void main(String para[]) {
            Untitled15 u = new Untitled15();        List l = new ArrayList();        Byte b1 = new Byte((byte) 1);
            Byte b2 = new Byte((byte) 2);
            Byte b3 = new Byte((byte) 3);        l.add(b1);
            l.add(b2);
            l.add(b3);
            byte byteArray[] = new byte[l.size()];
            Iterator temp = l.iterator();        while (temp.hasNext()) {
                int i = 0;
                byteArray[i] = ((Byte) temp.next()).byteValue();//这样转型成byte
                System.out.println(byteArray[i]);
                i = i + 1;        }    }}
      

  2.   

    可以直接从ArraysList里面直接取出来,然后把每一个元素在转成byte.
      

  3.   

    或者
        Object[] b = (Object[]) list.toArray();
        byte[] bb = new byte[b.length];
        for (int i = 0; i < b.length; ++i) {
          bb[i] = ( (Byte) b[i]).byteValue();
        }
      

  4.   

    就只有这种方法了哦..真有点失望...
    谢谢各位了...根据各位的提示
    得出了我解决问题的最终方法(JDK1.5):
    ArrayList<Byte> dataList = new  ArrayList<Byte>();
    .........
                      .........
    byte[] result = new byte[dataList.size()];
    int i = 0;
    for(Byte data : dataList) {
    result[i] = data.byteValue();
                                i++;
    }
      

  5.   

    本来ArrayList就只能存放Object,不能存放基础类型. 
    当然也不会提供直接转基础类型数组的方法