Java - 如何在一种方法、另一种甚至多种其他方法中使用数组?

Java - How to use an array in 1 method, in another or even multiple other methods?

我正在尝试在程序的所有其他方法中使用在 createArray 方法中创建的二维数组“myArray”。我查看了与此类似的其他一些堆栈溢出答案,但我尝试过的任何方法都没有奏效。每次我尝试将数组传递给其他方法时,它都无法解析。

这是我的代码:

import java.lang.Math;
import java.util.Scanner;


public class RandomArray {

    public static void main(String[] args) {
        createArray();
        calculateVariables();
        callArray();
    }

    public static void createArray(){
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
        int rows = scan.nextInt();
        System.out.print("Enter number of columns: ");
        int columns = scan.nextInt();

        int count = 0;
        int[][] myArray = new int[rows][columns];

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < columns; col++) {
                myArray[row][col] = (int) (Math.random() * 100);
                System.out.print(myArray[row][col] + "\t");

                if (rows > 0) {
                    count++;
                }
            }
            System.out.println();
            System.out.println();
            System.out.println("The number of elements in the array: " + count);
        }
    }
    
    public static void calculateVariables(){
        int sum = 0;
        int min = myArray[0][0];
        int max = myArray[0][0];
        int below50 = 0;

        for (int i = 0; i < myArray.length; i++) {
            for (int j = 0; j < myArray.length; j++) {

                if (max < myArray[i][j]) {
                    max = myArray[i][j];
                }

                if (min > myArray[i][j]) {
                    min = myArray[i][j];
                }

                sum = sum + myArray[i][j];

                if (myArray[i][j] < 50) {
                    below50++;
                }
            }

        }
        System.out.println("The average number is: " + sum / count);
        System.out.println("the min is " + min);
        System.out.println("the max is " + max);
        System.out.println("the number of entries below 50 is: " + below50);
        System.out.println();
        }
        
    public static void callArray(){
        Scanner scan2 = new Scanner(System.in);
        System.out.print("Enter a row number: ");
        int row1 = scan2.nextInt();
        System.out.print("Enter a column number: ");
        int col1 = scan2.nextInt();

        if ((row1 > myArray.length) && (col1 > myArray.length)) {
            System.out.println("Sorry, I cant retrieve that value! You entered a bad index.");
        }

        if ((row1 < 0) && (col1 < 0)) {
            System.out.println("Sorry, I cant retrieve that value! You entered a bad index.");
        } else System.out.println("The entry at that row and column is: " + myArray[row1][col1]);
        }
    }
}

您正在 createArray 方法的范围内创建该数组,这意味着 myArray 是仅在该方法中定义的变量。如果您想在其他方法中使用它,您可以在 main 方法中将其定义为 createArray 的 return 值,并在每次调用中传递它:

int[][] myArray = createArray();
calculateVariables(myArray);
callArray(myArray);

或将其定义为 class 字段:

static int[][] myArray;

public static void main(String[] args) {
    createArray();
    calculateVariables();
    callArray();
}

public static void createArray(){
    //... 
    myArray = new int[rows][columns];
    //... 
}

有两种方法可以做到这一点:

  1. 在您的 class 中将 myArray 设为静态字段,这样 class 中的每个静态方法都可以看到它。
  2. Return myArray 来自 createArray 方法,将其存储在 main 中的局部变量中,并将其作为参数传递给每个其他方法。

即:

static int[][] createArray() { ... }
static void calculateVariables(int[][] myArray) { ... }
...
int[][] myArray = createArray();
calculateVariables(myArray);
callArray(myArray);

选项 2 远优于选项 1,因为它:

  • 明确函数所依赖的数据
  • 防止您在未创建数组的情况下调用 calculateVariables
  • 允许您同时使用多个数组