12

How can I declare an array of byte arrays with a limited size for the array? This is what I was thinking but it's not working and I couldn't find anything.

private Integer number =10000;
private byte[] data[];
data = new byte[][number];

2 Answers 2

16

Something like this?

private byte[][] data;  // This is idiomatic Java

data = new byte[number][];

This creates an array of arrays. However, none of those sub-arrays yet exist. You can create them thus:

data[0] = new byte[some_other_number];
data[1] = new byte[yet_another_number];
...

(or in a loop, obviously).

Alternatively, if they're all the same length, you can do the whole thing in one hit:

data = new byte[number][some_other_number];
Sign up to request clarification or add additional context in comments.

5 Comments

Yes. Exactly. Just to clarify, does that mean that I will have "number" byte arrays, each with an undetermined size?
@gtdevel: In that first code snippet, you will have an array of length number. Each element of that array is a reference to a byte array, and is initialised to null. i.e. data[0] == null is true.
@Zéychin: Not exactly. See my above comment; it means you have "number" references to byte arrays; the byte arrays don't actually exist yet.
@OliCharlesworth I understand what you are saying. Thanks.
@OliCharlesworth: You are correct. I've forgotten my place, as I too often discuss these sorts of things with people who know these details and so there it is often acceptable to omit them in description for brevity. +1 for the correction for the clarity of the original poster.
3

may be you need a 2-d array

private byte[][] data = new byte[10][number];

that declares 10 byte arrays each of size number

2 Comments

And if I leave the "number" section blank, does that mean that the size could vary with each byte?
yes @gtdevel your understanding is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.