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

Time:09-26

I am trying to use the 2D array "myArray" which is created in the createArray method - in all other methods in the program. I looked over some other stack overflow answers similar to this, and nothing I have tried has worked. Every time I try to pass the array to the other methods, it is unresolved.

Here is my code:

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]);
        }
    }
}

CodePudding user response:

You're creating that array in the scope of the createArray method, that means that myArray is a defined variable only within that method. If you'd like to use it in other methods, you can either define it inside the main method as the return value of createArray and pass it in each call:

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

or define it as a class field:

static int[][] myArray;

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

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

CodePudding user response:

There are two ways to do this:

  1. Make myArray a static field in your class, so every static method in the class can see it.
  2. Return myArray from the createArray method, store it in a local variable in main, and pass it as a parameter to each of the other methods.

i.e.:

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

Option 2 is far superior to option 1, as it:

  • makes it clear what data the functions depend on
  • prevents you calling calculateVariables without having created the array.
  • allows you to have many arrays used at once
  • Related