Java – Converting ArrayList of bytes to byte[]

java

I'm looking to convert an ArrayList of bytes to byte[] in order to create a file with these bytes later. I've searched high and low on how to do this but still cant get my head around it. How can this be done?

Best Answer

There is no such thing as an ArrayList<byte>. When you declare one, you get an ArrayList<Byte>. So there's no single function idiom for turning this into byte[]. You'll have to allocate the byte[] and then write a loop to copy the values.

Or, you could use the fastutil library which does have a container like ArrayList that stores byte and which can yield an array.