E
E
EEElice2019-08-13 12:00:28
Java
EEElice, 2019-08-13 12:00:28

Why is an array in java filled in an incomprehensible way?

There is a little code that creates a 4x4 array and fills it with numbers from zero to 15.

static void show(int[][] m){
        for (int i=0;i<m.length;i++){
            for (int j=0;j<m.length;j++){
                m[i][j]=(int)(10*Math.random());
                System.out.print(m[i][j] + " "); }
            System.out.println();}
    }

static void shit(){
        int n = 4, q = 0;
        int[][] a = new int[n][n];
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                a[i][j] = q;
                q++;
            }
        }
        show(a);

The problem is that I don't understand what is displayed, for example:
/*
5 9 0 8 
6 6 1 7 
9 5 5 7 
8 3 7 1 
Или вот это, запустил только что
4 3 2 0 
0 2 5 2 
1 9 4 2 
3 7 0 4
Что должно выводиться по идее:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
*/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ronald McDonald, 2019-08-13
@EEElice

m[i][j]=(int)(10*Math.random());
This.
It is not entirely clear why it would fill with numbers from 0 to 15 if the array is filled randomly.
Replace this line with a variable that increments by one after each iteration.

E
EEElice, 2019-08-13
@EEElice

I accidentally forgot to remove that piece of code with random *facepalm*

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question