简体   繁体   中英

How to create a random matrix using arrays

I wanted to create a random 5x5 matrix using arrays and the Arrays.fill method. This is what I did:

import java.util.*;

class RandomMatrix {
    public static void main (String  [] args) {

        int i,j;
        int [] [] matrix = new int [5] [5];
        Arrays.fill (matrix, (int) Math.random()*10);

        for (i=0; i<matrix.length; i++) {
            for (j=0; j<matrix[i].length; j++) {

                System.out.printf("%-5d", matrix [i][j]);
            }           
            System.out.println();
        }           
    }
}

I actually thought it would work this way but now i get this error:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    at java.util.Arrays.fill(Unknown Source)
    at RandomMatrix.main(RandomMatrix.java:8)

Use the same loop structure that you use for printing the matrix.

for (int i=0; i<matrix.length; i++) {
    for (int j=0; j<matrix[i].length; j++) {
        matrix[i][j] = (int) (Math.random()*10);
    }           
}

Arrays.fill() works on arrays, your matrix is an array consisting of arrays. Even if you used something like Arrays.fill (matrix[0], (int) Math.random()*10); , you would put the same (randomly chosen) value into each cell of row 0.

I think you have got an error because method fill() can't work with the multi-dimensional array. Just convert it look like :

for(int k = 0;k<5;k++){
        int[] example = matrix[k];
        Arrays.fill (example, new Random().nextInt(10)*10);
 }
    int[][] m = new int[5][5];

    //https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#fill-int:A-int-
    //public static void fill(**int[]** a,  int val)
    for(int[] r : m)
        Arrays.fill(r, (int) (Math.random()*10));

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m.length; j++) {
            System.out.print(m[i][j] + " ");
        }
        System.out.println("");
    }

    System.out.println("Second");

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m.length; j++) {
            m[i][j] = (int) (Math.random()*10);
        }
    }       

    for (int i = 0; i < m.length; i++) {
        for (int j = 0; j < m.length; j++) {
            System.out.print(m[i][j] + " ");
        }
        System.out.println("");
    }
Output:
3 3 3 3 3 
4 4 4 4 4 
7 7 7 7 7 
2 2 2 2 2 
8 8 8 8 8 
Second
0 0 4 9 1 
5 8 6 3 8 
5 7 3 5 1 
1 1 6 4 8 
6 2 1 4 0 

Arrays.fill() fills an array. Your matrix is an array of arrays, so instead of writing

Arrays.fill (matrix, (int) Math.random()*10);

you could also write

int a = (int) Math.random()*10;
matrix[0] = a; //doesn't work, matrix[0] is an int array!
matrix[1] = a; //doesn't work, matrix[0] is an int array!

and so on. See why it doesn't work? It's wrong in two ways. First, fill() doesn't support nested arrays, and second, fill takes a value as parameter, not a supplier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM