-1

I want to create an array of byte[]. When i try:

byte[] data = new byte[10];

This is an array of byte and not an array of byte[]. Thanks

Alnitak
  • 334,560
  • 70
  • 407
  • 495
Alex
  • 73
  • 1
  • 1
  • 8

5 Answers5

6

To address some confusion about "arrays of arrays" and 2D arrays:

Java doesn't really have 2D arrays. At least, not in the way many people think of them, where they look like a matrix, and you have rows and columns and the same number of elements in each row.

You can use an "array of arrays" as a 2D array. If you want an array of 5 x 8 integers, you can say

int[][] arr = new int[5][8];

But Java does not enforce the "matrix-ness" or "2D-ness" of an array. What it gives you is an array of 5 references, each of which refers to another array of integers. Each reference starts out referring to an array of 8 integers, but this is not enforced; you could later say

arr[2] = new int[113];

or

arr[2] = null;

and now your array is no longer really a 2D array (as we usually think of it), but Java will not stop you from doing that.

So while you may often see something like

byte[][] 

used in textbooks to create a 2D array, it's the same syntax as an array of arrays, and Java actually treats it as an array of arrays, not all of which have to be the same size.

If you want to create an array whose elements will eventually be arrays, but that are null for now, you can do it like this:

byte[][] bytesArray = new byte[10][];

which sets bytesArray to an array of 10 null references, any one of which could be set later:

bytesArray[4] = new byte[17];

That would be the way to start, if you want a "ragged array" of arrays that aren't all the same length.

ajb
  • 31,309
  • 3
  • 58
  • 84
5

In Java, an array where each element is itself an array is effectively just a 2D array, although Java doesn't require that each row have the same number of elements.

In this particular case where you have a List<byte[]> and you wish to turn that into an array where each element of the array is itself a byte array you could use:

int rows = myList.size();
byte[][] myArray = new byte[rows][];

int i = 0;
for (byte[] col : myList) {
    myArray[i++] = col;
}

or, more succinctly, but less obviously:

byte[][] myArray = myList.toArray(new byte[0][]);

which in use looks like:

List<byte[]> myList = new ArrayList<>();
myList.add(new byte[]{1,2,3,4});
myList.add(new byte[]{5,6,7});
myList.add(new byte[]{8,9});

byte[][] myArray = myList.toArray(new byte[0][]);

for (byte[] b : myArray) {
    for (byte c : b) {
        System.out.println(c);
    }
    System.out.println();
}

Note that in both cases above the elements of the array are exactly the same arrays as those stored in the list, not a copy. Therefore if you modify one of the original byte[] elements in the list it'll also change the version accessible via the array.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
2

Try this:

byte[][] arrays = new byte[5][10];

  • This is a 2D array. I want an array of byte[], like you can do an array of int. – Alex Sep 03 '14 at 19:49
  • 3
    @Alex that's what an array of `byte[]` is - a 2D array – Alnitak Sep 03 '14 at 19:50
  • @Alnitak No!!, i have a List and i want to conver this to an array. So i should create an array of byte[] , and i don't want 2D array. Simple question – Alex Sep 03 '14 at 19:52
  • array of array is a2d array. – Samuele Panarotto Sep 03 '14 at 19:53
  • 3
    @Alex everyone here is telling you the same thing, and they are correct: an array of arrays is a two-dimensional array. – Bobulous Sep 03 '14 at 19:53
  • Like this? Foo[] array = list.toArray(new Foo[list.size()]); – Samuele Panarotto Sep 03 '14 at 19:55
  • @SamuelePanarotto Edit your answer to change `Byte` to `byte` and I'll upvote your answer, as it is otherwise correct. (And hopefully the three downvoters will copy suit, or at least explain themselves.) – Bobulous Sep 03 '14 at 19:55
  • @SamuelePanarotto the OP is using `byte`, not `Byte` - not sure how to use `.toArray` (if indeed it's possible) with a primitive. – Alnitak Sep 03 '14 at 20:00
  • To the OP, when you change your answer (because it's wrong) you should note that in the comments so that down votes can be modified (if appropriate) – KevinDTimm Sep 05 '14 at 13:53
1

You mean like this?

byte[][] data = new byte[][]{byteArray1, byteArray2, byteArray3};
RockOnRockOut
  • 751
  • 7
  • 18
  • Like this: byte[] _______ byteArray = {-119, -8, -113, 118, -127} – Alex Sep 03 '14 at 19:51
  • 4
    @Alex you appear confused - what you've just written is a 1D array of `byte`, not an array of `byte[]` – Alnitak Sep 03 '14 at 19:52
  • 1
    This answer does describe an "array of byte arrays", exactly as requested in your question. – Bobulous Sep 03 '14 at 19:52
  • What you just described is an array of byte, not byte[]. An array of byte[] is just a 2D array – RockOnRockOut Sep 03 '14 at 19:53
  • @Alnitak if i make byte[][] is 2D array and not 1D array of byte[]. – Alex Sep 03 '14 at 19:53
  • 1
    @Alex yes, because that's what an array of `byte[]` is - a 2D array! I shall re-open your question and elaborate – Alnitak Sep 03 '14 at 19:54
  • @Alex I just saw that you want to convert a List to an array of arrays. In this case what you want is a 2D array, which is the same thing as an array of arrays – RockOnRockOut Sep 03 '14 at 19:55
  • 2
    @Alex In Java there is no difference between an "array of arrays" and a 2D array. There are some languages that have separate concepts for those two things, but Java is not one of them. – ajb Sep 03 '14 at 19:59
1

In your example, you are creating an array of bytes, if you wanted to create an array of arrays of bytes you would have to create a two dimensional array of bytes:

byte[][] bytesArray = new byte[10][10];

You would then be able to access your array of bytes as follows:

bytesArray[0]; // returns a byte[]

Lawrence Andrews
  • 440
  • 4
  • 12