2

I would like to know how to properly reference an Array Of An Array of Bytes

I have some primitive data types, which are say a couple of megabytes large each

byte[] data1, data2, data3, data4 ... ,data10;

data1 = ..(something 2MB big)
data2 = ..(something 2MB big)
data3 = ..(something 2MB big)
.
.
data10 = ..(something 2MB big)

I want to arrange these 10 byte[]'s into an array (this is where my code goes wrong)

private byte[] arrayofdatas[];
arrayofdatas[1] = data1;
arrayofdatas[2] = data2; 
.
.
etc.

I've tried this but when the line arrayofdatas[1] = data1 goes, I get a null pointer exception Please help me clean up the code, it has been very hard to search about this exact problem.

P.S. I need the data types to remain as byte[]. Please do not convert them into Strings.

Edit: I need the variable arrayofdatas to be a field variable. I'm having problems because I don't know how to declare it as field. All your answers have made it local and used 'new'. Another thing.. I have made '10' an abitrary number of byte[]'s that I want. Ultimately I want this array to be unbounded. Is there a way to add on to the array as more byte[]s are added, so that there could be a arrayofdata[500] in the future, without having to declare the size at the start?

6 Answers 6

4
private byte[][] arrayofdatas = new byte[10][];

And you can use them in your method.

arrayofdatas[0] = data1;
arrayofdatas[1] = data2;
.
.
etc.

Note that you should declare arrayofdatas as a field of class. Or if you want to declare it in method you should remove private modifier.

Sign up to request clarification or add additional context in comments.

1 Comment

If its 10 data arrays, and you start with index 1 in arrayofdatas, you should allocate 11 data arrays: new byte[11][];
3

Your array is not allocated properly. If you need a bidimensional array use:

byte[][] arrayofdatas = new byte[10][];
arrayofdatas[1] = data1;
arrayofdatas[2] = data2; 
.
.
etc.

Comments

1
byte data[][] =  new byte[10][];

data[0]= ..(something 2MB big)

etc

Comments

0

If I understand you correctly you need 2 dimensional array.

byte[][] arr2d = new byte[][];
byte [] data1, data2, ....;
arr2d[0] = data1;
arr2d[1] = data2;
///..............

I hope this helps although your question probably signals me that your design is not so ideal. I'd recommend you to post here description of your task and how you are designing it. Probably guys can recommend you how to improve your design.

Comments

0

Why not do something like this:

byte[][] data = new byte[10][];
for(int i=0; i<10; i++)
{
    byte[i] = ...  //fill your data here;
}

Comments

0

Have you tried multi-dimensional arrays? I remember using them for String. If they did for String ... they should for any other datatype as long as you are assigning byte data :)

This should help.

byte[][] arrayofdatas = new byte[10][];
arrayofdatas[1] = what you wanted for data1 ..(something 2MB big)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.