1楼的做法是有问题的,看看toArray的源码就知道了,如果传入的数组长度为0的话,则没有作用。
应改为
Byte[] b = new Byte[arr.size()];
arr.toArray(b);
另外要说的是转换回来的数组并不是byte基本类型的数组,而是Byte类型的数组。

解决方案 »

  1.   

    还是采取最傻的办法,for循环吧,依次把值赋给byte数组
      

  2.   


    public static String[] toStringArray(List<String> strList) {
           String[] array = new String[strList.size()];
           strList.toArray(array);
           return array;
          }
        public static void main(String[] args) {
         List<String> strList = new ArrayList<String>();
         strList.add("Monkey");
         strList.add("Cat");
         strList.add("Pig");
         strList.add("Rabbit");
    toStringArray(strList);
    for (int i = 0; i < toStringArray(strList).length; i++) {
    System.out.println(toStringArray(strList)[i]);
    }
    }
      

  3.   

    ArrayList<Byte> arr = new ArrayList<Byte>();
    arr.add(new Byte((byte)1));
    arr.add(new Byte((byte)2));
    arr.add(new Byte((byte)3));
    System.out.println(arr.toArray(new Byte[0]));
    for(byte b:arr.toArray(new Byte[0])){
    System.out.println("-->"+b+"<--");
    }
    明显是可以的嘛
      

  4.   

    刚才顺手点开了ArrayList的toArray方法
    public Object[] toArray(Object aobj[])
        {
            if(aobj.length < size)
                return (Object[])Arrays.copyOf(elementData, size, ((Object) (aobj)).getClass());
            System.arraycopy(((Object) (elementData)), 0, ((Object) (aobj)), 0, size);
            if(aobj.length > size)
                aobj[size] = null;
            return aobj;
        }
    可以看到如果传一个长度小于list size的数组 会调用 Arrays.copyOf方法
    否则 调用System.arraycopy方法
    如果长度大于list.size 会把copy完成的数组的第length个对象置为null
      

  5.   

    很简单呀,list有api可以直接转数组的。 ArrayList<Byte> arr = new ArrayList<Byte>();
    arr.add(new Byte((byte) 1));
    arr.add(new Byte((byte)2));
    arr.add(new Byte((byte)3));

    Byte[] bytes =  new Byte[arr.size()];
    arr.toArray(bytes);
      

  6.   

    System.out.println(Arrays.toString(bytes)); 这个可以直接打印出值,不要循环了。
      

  7.   

    int len=arr.length;
    Byte[] result=new Byte[len]
    while(--len>=0){
    result[len]=arr.get[len];
    }
      

  8.   

    NEW一个字节数组 循环接收
    int len=arr.length;
     Byte[] result=new Byte[len]
     while(--len>=0){
     result[len]=arr.get[len];
     } 
      

  9.   

    ArrayList<Byte> arr = new ArrayList<Byte>();
     arr.add(new Byte((byte)1));
     arr.add(new Byte((byte)2));
     arr.add(new Byte((byte)3));
     System.out.println(arr.toArray(new Byte[0]));
     for(byte b:arr.toArray(new Byte[0])){
     System.out.println("-->"+b+"<--");
     }
    这样也是可以的
    不过改动成这样就不行了
     ArrayList<String> al = new ArrayList<String>();
      al.add("th");al.add("is");al.add("oo");
      String []  s=new String[0];
      al.toArray(s);
      for(String st:s)System.out.println(st);必须改成String [] s = new String[3];