Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ArrayList<Byte> into a byte[] [duplicate]

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[].

like image 802
fulmicoton Avatar asked Jul 28 '11 13:07

fulmicoton


2 Answers

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.

like image 131
Michael Borgwardt Avatar answered Sep 29 '22 10:09

Michael Borgwardt


After calling toArray() you can pass the result into the Apache Commons toPrimitive method:

https://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/ArrayUtils.html#toPrimitive-java.lang.Byte:A->

like image 29
JustinKSU Avatar answered Sep 29 '22 10:09

JustinKSU