简体   繁体   English

如何使用数组创建随机矩阵

[英]How to create a random matrix using arrays

I wanted to create a random 5x5 matrix using arrays and the Arrays.fill method. 我想使用数组和Arrays.fill方法创建一个随机的5x5矩阵。 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. Arrays.fill()适用于数组,您的矩阵是由数组组成的数组。 Even if you used something like Arrays.fill (matrix[0], (int) Math.random()*10); 即使您使用了Arrays.fill (matrix[0], (int) Math.random()*10); , you would put the same (randomly chosen) value into each cell of row 0. ,您可以将相同(随机选择)的值放入第0行的每个单元格中。

I think you have got an error because method fill() can't work with the multi-dimensional array. 我认为您遇到了错误,因为方法fill()无法与多维数组一起使用。 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. Arrays.fill()填充数组。 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. 首先,fill()不支持嵌套数组,其次,fill将值作为参数而不是供应商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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