简体   繁体   中英

How to make an Array Accessible from instantiation in main class? Accessible to all classes in same class

I want to run a method that returns an array . Code such as this:

public static int[] getArray() {        
    int square[] = new int[5];
    int input = 0;

    System.out.println("Input a valid integer from 1-49");
    System.out.println("for array input please \\(^-^)/");
    System.out.println("Remember (^_'), don't repeat numbers");

    Scanner reader = new Scanner(System.in);

    for (int i = 0; i < 5; i++) {
        System.out.println(
            "Please input the integer for position " + (i + 1) + " of the array");
        input = reader.nextInt();
        square[i] = input;
    }

    return square;
}

I have researched that you can make a variable like so int[] data = getArray(); How would I make it so that data can be accessible to other methods in the same class so I could do something like

public static int linearSearch(data) {
}

without having to constantly be re-entering the values for the array?

You need to declare i your array like this:

public YourClass {
    public static int[] square = new int[5];

}

This way you can access this array from any other class and it will remain with the exact array (that's what static for). Example:

From Class1 - YourClass.square From Class2 - YourClass.square Both are the same array instance

You can try out to introduce private variable of int[] and provide a lazy initialization for it, something like this:

class aClass {
    int[] data; // default to the null

    private int[] getArray() {
         if (data == null) {
             // your console logic for initialization
         }
         return data;
    }

    public static int linearSearch() {
         int[] localData = getArray();
    }
}

But in this case you can change the contents of data field in your methods across the class.

This can be done two ways:
- Either declaring the variable as class-level variable
- Or declaring it as local variable inside main method

public class ReturnIntArraysSO {

 /** * @param args */ public static void main(String[] args) { int[] data = getArray(); for(int i : data){ System.out.print(i+" "); } linearSearch(data); } /** * * @return */ public static int[] getArray() { int square[] = new int[5]; int input = 0; System.out.println("Input a valid integer from 1-49"); System.out.println("for array input please \\\\(^-^)/"); System.out.println("Remember (^_'), don't repeat numbers"); Scanner reader = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.println("Please input the integer for position " + (i + 1) + " of the array"); input = reader.nextInt(); square[i] = input; } return square; } /** * * @param data * @return */ public static void linearSearch(int[] data) { for(int a : data){ if(a == 5){ System.out.println("\\nFound 5!!"); } } } 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM