Convert ArrayList<Byte> into a byte[]

fulmicoton picture fulmicoton · Jul 28, 2011 · Viewed 40.5k times · Source

Possible Duplicate:
How to convert an ArrayList containing Integers to primitive int array?

How to convert an ArrayList<Byte> into a byte[]?

ArrayList.toArray() gives me back a Byte[].

Answer

Michael Borgwardt picture Michael Borgwardt · Jul 28, 2011
byte[] result = new byte[list.size()];
for(int i = 0; i < list.size(); i++) {
    result[i] = list.get(i).byteValue();
}

Yeah, Java's collections are annoying when it comes to primitive types.