CSharpLover
CSharpLover

Reputation: 91

Java: byte[] to Byte[]

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

Upvotes: 5

Views: 8187

Answers (4)

Aison
Aison

Reputation: 71

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

Upvotes: 1

Daan Gerits
Daan Gerits

Reputation: 31

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[]);

Upvotes: 3

Jay
Jay

Reputation: 27474

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.

Upvotes: 3

CSharpLover
CSharpLover

Reputation: 91

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

Upvotes: 4

Related Questions