2d byte array of numbers
This is not possible; in java, arrays are not extensible (you can't 'make your own array' or e.g. write class MyArray extends int[]
or some such, nor can you make a custom definition of what the foo[x]
operator does), and arrays are strictly 1 dimensional.
However, you can, of course, make an array whose component type is itself an array. Imagine one of those tool boxes that contains lots of boxes. A box of boxes.
There is, for example, absolutely no need in java for these 'component arrays' to be the same size. Whereas with a 2D array, they'd have to be the same size:
int[][] x = new int[5][];
x[0] = new int[10];
x[1] = new int[20];
In that code, we have a big box that has room for 5 little boxes. The first little box is a smaller one capable of holding 10 screws. The second box is a larger one, with room for 20 screws. The spots where box 3, 4, and 5 go, are empty.
This boxes-in-boxes arrangement, which does have some syntactic sugar support in java, is sometimes called '2D arrays' as a slang term. But, that is a fake term and it will lead you astray if you think of int[][]
as a '2d array'. It's an array of int arrays.
Simple example:
int[][] x = new int[10][5]; // this works, is syntax sugar.
x.getClass().getComponentType() == int[].class; // this is true.
if you think about x
as a 2D array you'd think its component type is int
. But it's not a 2D array, it's an array of int arrays, so its component type is "int array".
The specific problem you're running into, is that you're setting an individual screw location (array[i][j]
means: Open the big box, and go to the i-th spot for a little box. Then open that box, and go to the j-th spot where screws can go).. and then you're trying to put an entire box of screws down in the spot where only a single screw can go.
nextBytes
doesn't return a byte array either, it returns nothing; instead, you pass it a byte array (and not an array of byte arrays, not a single byte!) and it fills it up with random data. You also already made an instance of Random. There is no need to make more.
Thus, replace:
array[i][j] = new Random().nextBytes(array);
with:
rnd.nextBytes(array[i]);
and, entirely remove the for (int j = 0
loop.
Alternatively, you can keep it all, and replace the line with:
array[i][j] = (byte) rnd.nextInt(256);
Here you're generating the bytes yourself, one at a time, instead of using nextBytes
which does some of that for you.