【问题标题】:Java - How to use an array in 1 method, in another or even multiple other methods?Java - 如何在一种方法、另一种甚至多种其他方法中使用数组?
【发布时间】:2021-11-18 15:19:19
【问题描述】:

我正在尝试使用在 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]);
        }
    }
}

【问题讨论】:

  • 您永远不会将数组传递给任何其他方法。由于数组被定义为方法局部变量,其他方法无法访问它,除非它被传递
  • 显而易见的答案,特别是对于名为“创建数组”的方法,是返回您刚刚创建的数组:int[][] createArray(){...

标签: java arrays multidimensional-array methods


【解决方案1】:

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

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

或将其定义为类字段:

static int[][] myArray;

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

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

【讨论】:

  • 考虑到方法createArray() 是静态的,您需要在第二个示例的数组定义前面添加static 修饰符。
  • 是的,好点。从我的脑海中溜走
  • 非常感谢!我选择了第二个示例 - 类字段。
【解决方案2】:

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

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

即:

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

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

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

【讨论】:

  • 您能否提供选项二的代码示例?我认为这样会更清楚:)
  • 感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 2021-08-11
  • 2012-03-22
  • 2011-07-05
  • 2012-03-10
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
相关资源
最近更新 更多