这个阵列怎么了?

时间:2017-01-28 20:47:09

标签: java arrays multidimensional-array

public class lab {
    public static void main (String args[]){
        double[][] g = {RandomArray(3)};
        printArray(g);
    }

    private static void printArray(double[][] g) {
        System.out.println(Arrays.deepToString(g));
    }

    public static double[][] RandomArray(int n) {

        double[] [] RandomArray = new double[n] [n];
        Random randomNumberCreator = new Random();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                RandomArray[i][j] = randomNumberCreator.nextDouble() * 100;
            }
        }
        return RandomArray;
    }
}

我不确定我的RandomArray方法有什么问题,我希望它适用于二维数组,但我明显犯了一个错误,因为下面的行正在收到错误,我不确定为什么会发生这种情况。如果你能向我解释我所犯的错误,我将不胜感激。

  

double [] [] g = {RandomArray(3)};

2 个答案:

答案 0 :(得分:2)

删除函数Call of&#34; RandomArray&#34;

周围的花括号
  public static void main (String args[]){
        double[][] g = RandomArray(3);
        printArray(g);
    }

    private static void printArray(double[][] g) {
        System.out.println(Arrays.deepToString(g));
    }

    public static double[][] RandomArray(int n) {

        double[] [] RandomArray = new double[n] [n];
        Random randomNumberCreator = new Random();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                RandomArray[i][j] = randomNumberCreator.nextDouble() * 100;
            }
        }
        return RandomArray;
    }

答案 1 :(得分:0)

您正在错误地初始化数组.... 调用方法 RandomArray

时,您不需要 {}

只需执行double[][] g = RandomArray(3);即可完成工作