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
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.
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.
Try this:
byte[][] arrays = new byte[5][10];
You mean like this?
byte[][] data = new byte[][]{byteArray1, byteArray2, byteArray3};
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[]