Java: byte[] to Byte[]
Asked Answered
I

4

5

Java makes me sad since it needs wrapper classes for ArrayLists. How would I go about adding a byte[] to a ArrayList<Byte[]>?

Infinitive answered 11/2, 2010 at 21:25 Comment(1)
What is wrong with ArrayList<byte[]>? That works (as a list of byte[] arrays). Perhaps you meant ArrayList<Byte> from a byte[]?Lawsuit
I
4

LOL thought I had to wrap everything. ArrayList<byte[]> works. Thanks Yishai.

Infinitive answered 11/2, 2010 at 21:31 Comment(0)
O
3

You have to wrap any primitives to use them in a context that requires an object. But a byte[] is not a primitive. It's an array of bytes, and an array is an object.

Just to clarify: Do you really want an ArrayList of arrays of bytes, i.e. effectively a two-dimensional array? Or do you really simply want an ArrayList of bytes? In that case, you would have to wrap the bytes in Bytes to put them in the ArrayList.

Octahedron answered 11/2, 2010 at 22:37 Comment(0)
B
3

Just for the purpose of others searching for this, if you have Apache Commons on your classpath, you can do something like the following to get Byte[] back (documentation]:

Byte[] result = ArrayUtils.toObject(byte[]);
Beggs answered 16/1, 2013 at 17:36 Comment(0)
R
1

ArrayList works only, if you do not require the results of hashCode() and equals() on this list.

Rathbun answered 29/5, 2015 at 0:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.