Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting ArrayList of bytes to byte[]

Tags:

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?

like image 641
DMo Avatar asked Jun 08 '26 00:06

DMo


1 Answers

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.

like image 177
bmargulies Avatar answered Jun 10 '26 15:06

bmargulies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!