Actually there is:

ArrayList<Byte> bytes = ... 

It is necessary to convert to:

 byte[] lol = ... 

it is clear that you can like this:

  byte[] lol = new byte[bytes.size()]; int inclol = 0; for (Byte b : bytes) { lol[inclol] = b.byteValue(); inclol++; } 

This code is the norm is that? or is there some sugar for similar situations that i missed? :) It is clear that it is impossible to bring a typed collection to an array right away, but this is why Byte [] (obtained from the collection) to byte [] is not given even as something strange ... autoboxing ignores this situation, I did not find static methods in util .. . :)

    2 answers 2

     ArrayList<Byte> bytes= new ArrayList<Byte>(); byte[] b= ArrayUtils.toPrimitive( bytes.toArray(new Byte[bytes.size()]) ); 

    True ArrayUtils are not in the JRE.

    • apache commons will also go, there are so many useful things there that it can be considered native Java :) - skobets
    • The funny thing is that in the source code of ArrayUtils - everything is solved by direct array lookup :) - Barmaley

    Byte[] and byte[] are different types. One is not a wrapper for the other. Auto boxing / unboxing is applicable only to primitive types and their wrappers.

    And by the way, you can get an array of elements from ArrayList 'and you can use the toArray method.

    • Well, I wrote the same thing that I know about toArray, but it will return either an array of Byte [] or an array of Object [], Byte wrapper byte, I expected some kind of sugar for their arrays, but I did not find it :) - skobets