Skip to content
Advertisement

Create a List of byte[]

How might you go about creating a List of byte[] (not Byte)?

I want something like the following:

byte[] deb = new byte[Byte.MIN_VALUE];
List<byte[]> begin = new LinkedList<>();
begin.add(deb);

Advertisement

Answer

That will work fine because arrays are objects in Java, so you can build Lists out of them.


Note that only in Java 7 can you do

List<byte[]> begin = new LinkedList<>();

In older versions you must restate the byte[]:

List<byte[]> begin = new LinkedList<byte[]>();

This has been brought up already but I’ll just reiterate it here. Byte.MIN_VALUE is less than 0; you cannot create an array of length less than 0 (it results in a runtime error, specifically a NegativeArraySizeException). Did you mean Byte.MAX_VALUE?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement