0

I'm just starting to learn programming and I can't solve the problem, I need to create a 2d byte array of numbers, and the number of rows and columns is set by the user. Thanks in advance!

import java.util.*;
class Simple{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        System.out.println("enter the vertical and horizontal length of the matrix:");
        int m = sc.nextInt();
        int n = sc.nextInt();
        byte[][] array = new byte[n][m];
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                array[i][j] = new Random().nextBytes(array);
            }
        }
        System.out.println(array);
        }
    }

here is my effort. I tried to do the same as with whole numbers, but it doesn't work, I couldn't find other options anywhere.

  • have the same topic, you can read the answer [here](https://stackoverflow.com/a/25652826/14705777) – blqck Apr 12 '23 at 19:22

2 Answers2

3

You have four issues in your code. First, valid array indices are 0 to array.length - 1 so you need < not <= in your loop. Second, you already have a Random instance use it; never create new Random instances in a loop they are likely to reseed with the same value on multiple iterations. Third, the call Random#nextBytes(byte[]) fills the array - it doesn't return a single byte. Fourth, arrays don't override Object#toString() so you can't print it like that (use Arrays.deepToString). Putting that all together, should look something like

Scanner sc = new Scanner(System.in);
Random rand = new Random();
System.out.println("enter the vertical and horizontal length of the matrix:");
int m = sc.nextInt();
int n = sc.nextInt();
byte[][] array = new byte[n][m];
for (int i = 0; i < n; i++) {
    rand.nextBytes(array[i]);
}
System.out.println(Arrays.deepToString(array));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72