2

Is there a way to use the first function in the second one to create a double array with random numbers?

public  static  int[]  build1(int size) {
    int[] arr = new int[size];
    for (int i=0 ; i < arr.length ; i++)
        arr[i] = (int)(Math.random() * 127);

    return arr;
}

public  static  int[][] build2(int row, int col) {
    int[][] arr2 = new int[row][col];
    for (int i = 0; i < arr2.length; i++) {
        for (int j = 0; j < arr2[i].length; j++) {
            arr2[i][j] = (int)(Math.random() * 127);
        }
    }
    return arr2;
}

1 Answer 1

4

I would assume the following should work.

public static int[][] build2(int row, int col) {
    int[][] arr2 = new int[row][col];
    for (int i = 0; i < arr2.length; i++) {
        arr2[i] = build1(col);
    }
    return arr2;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.