简体   繁体   English

移动数组到不同的 class

[英]Moving an array to a different class

so im trying to get values of an array to a different class and to do stuff there.所以我试图将数组的值获取到不同的 class 并在那里做一些事情。 But i keep getting all those errors.但我不断收到所有这些错误。 Ive tried out so many things but it doesnt work.我尝试了很多东西,但没有用。 Basically i want each position of an arrray assign a value and pull these numbers to a different class and maybe later compare them.基本上我希望数组的每个 position 都分配一个值并将这些数字拉到不同的 class,然后可能会比较它们。 Its a simplified code of mine.它是我的简化代码。 I would thank anyone who can change the code so i dont get the error messages, i would appreciate if you could keep it as close to this as possible, because my code is very long and i want to get it to different classes to minimize the main class. Thanks in advance.我要感谢任何可以更改代码的人,这样我就不会收到错误消息,如果你能尽可能接近这个我将不胜感激,因为我的代码很长,我想把它放到不同的类中以最小化主要 class。提前致谢。 <3 <3

package Vasu;
public class First {

    public int n = 5;
    public int m = 0;
    long[] arr = new long[n];

    public static void main(String[] args) {

        Second C = new Second();  // cannot make a static reference in a non static field 
        arr[n - m] = m;           // "
        C.rules(arr[n - m]);      // "
        m++;

    }
}
//-----------------------------------------
package Vasu;

import java.util.Arrays;

public class Second {
    public double rule;

    public void rules(long[] arr) {
        if (arr[n - m] > 2) { // m and n cant be resolved into a variable
            rule=1;
            System.out.println("Array: " + Arrays.toString(arr));
        }
    }
}

Your error is that you pass a long variable instead of long array in line C.rules(arr[n - m]);您的错误是您在C.rules(arr[n - m]); , try pass the parameter C.rules(arr); ,尝试传递参数C.rules(arr); (without the [nm] ). (没有[nm] )。

In main() , you cannot access n , m , and arr because they are non-static.main()中,您无法访问nmarr ,因为它们是非静态的。 This could be "fixed" by changing them to static:这可以通过将它们更改为 static 来“修复”:

public class First {

    public static int n = 5;
    public static int m = 0;
    public static long[] arr = new long[n];

    public static void main(String[] args) {
        Second C = new Second();
        arr[n - m] = m;
        C.rules(arr);
        m++;
    }
    
}

I used quotes around "fixed" because we don't know how these values are being used in the larger context of your program.我在“固定”周围使用了引号,因为我们不知道这些值是如何在您的程序的更大上下文中使用的。 This may be fine, or it may break other parts...we'd have to understand a lot more about your program to make that call.这可能没问题,也可能会破坏其他部分……我们必须更多地了解您的程序才能进行调用。

Note that when calling c.rules(arr) , we simply pass the name of the array.请注意,在调用c.rules(arr)时,我们只是传递数组的名称。 The syntax you were trying to use with c.rules(arr[n - m]) was passing a single value, while the method expects a reference to an entire array.您尝试与c.rules(arr[n - m])一起使用的语法正在传递单个值,而该方法需要对整个数组的引用。

In class Second, we can now access n and m by preceding them with their class name, as in First.n and First.m .在 class 其次,我们现在可以通过在它们的 class 名称之前访问nm ,如First.nFirst.m This is what the static designation allows you to do:这是 static 指定允许您执行的操作:

public class Second {

    public double rule;

    public void rules(long[] arr) {
        if (arr[First.n - First.m] > 2) {
            rule=1;
            System.out.println("Array: " + Arrays.toString(arr));
        }
    }
    
}

An alternative approach would be to pass both n and m to rules() along with the array.另一种方法是将nm与数组一起传递给rules() With that approach, however, you would not be able to change n and m and have those changes be reflected back in class First.但是,使用这种方法,您将无法更改nm ,并且无法将这些更改反映在 class First 中。

If you can't have n , m , and arr as static members, then you need to create an instance of First within main() to access those members.如果不能将nmarr作为 static 成员,则需要在main()中创建First的实例以访问这些成员。 Doing so would also mean you could pass a reference to First into Second and access the values that way.这样做还意味着您可以将对 First 的引用传递给 Second 并以这种方式访问值。

These are tough design decisions that we can't make for you without a better understanding of your app.如果没有更好地了解您的应用程序,我们将无法为您做出这些艰难的设计决定。

You definitely need to master what it means to be static , or you're not going to progress very far with this problem.你肯定需要掌握static的含义,否则你不会在这个问题上取得太大进展。

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

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