简体   繁体   English

如何通过主类中的实例化使数组可访问? 可访问同一班级的所有班级

[英]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 . 我想运行一个返回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(); 我研究过,您可以像int[] data = getArray();这样的变量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 从Class1- YourClass.square从Class2- YourClass.square都是相同的数组实例

You can try out to introduce private variable of int[] and provide a lazy initialization for it, something like this: 您可以尝试引入int[]私有变量,并为其提供延迟的初始化,如下所示:

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. 但是在这种情况下,您可以在整个类的方法中更改data字段的内容。

This can be done two ways: 这可以通过两种方式完成:
- Either declaring the variable as class-level variable -将变量声明为类级变量
- Or declaring it as local variable inside main method -或在main方法中将其声明为局部变量

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!!"); } } } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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