将一个数组移到另一个类中

huangapple go评论78阅读模式
英文:

Moving an array to a different class

问题

所以我试图将数组的值传递给另一个类并在那里进行操作但是我一直在遇到所有这些错误我尝试了很多方法但都不起作用基本上我希望数组的每个位置分配一个值并将这些数字传递给另一个类也许以后可以进行比较这是我的简化版代码我会感谢任何能够修改代码的人这样我就不会收到错误消息如果您能尽量保持代码与此代码接近我会非常感激因为我的代码非常长我希望将其传递给不同的类以最小化主类提前致谢 <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();  
        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) {
        int n = 5;  // 在这里声明 n 和 m
        int m = 0;
        
        if (arr[n - m] > 2) { 
            rule = 1;
            System.out.println("Array: " + Arrays.toString(arr));
        }
    }
}
英文:

so im trying to get values of an array to a different class and to do stuff there. 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. 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. <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;           // &quot;
		C.rules(arr[n - m]);      // &quot;
		m++;

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

import java.util.Arrays;

public class Second {
	public double rule;

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


</details>


# 答案1
**得分**: 1

你的错误在于在第`C.rules(arr[n - m]);`行中传递了一个长变量,而不是一个长数组,请尝试传递参数`C.rules(arr);`(去掉`[n-m]`部分)。

<details>
<summary>英文:</summary>

Your error is that you pass a long variable instead of long array in line `C.rules(arr[n - m]);`, try pass the parameter `C.rules(arr);` (without the `[n-m]`).

</details>



# 答案2
**得分**: 1

在 `main()` 函数中,由于它们是非静态的,你无法访问 `n`、`m` 和 `arr`。你可以通过将它们改为静态来“修复”这个问题:

```java
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++;
    }
    
}

我在“修复”一词周围使用了引号,因为我们不知道这些值在程序的更大上下文中是如何使用的。这可能是没问题的,也可能会影响其他部分...要做出判断,我们需要更多地了解你的程序。

请注意,在调用 c.rules(arr) 时,我们只需传递数组的名称。你之前尝试使用 c.rules(arr[n - m]) 的语法是在传递单个值,而该方法需要一个对整个数组的引用。

在 Second 类中,我们现在可以通过在它们的类名前加上前缀来访问 nm,如 First.nFirst.m。这就是静态修饰允许你做的事情:

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

另一种方法是在将 nm 与数组一起传递给 rules() 时,同时传递它们。然而,通过这种方法,你将无法更改 nm 并在 First 类中反映这些更改。

如果不能将 nmarr 设置为静态成员,那么你需要在 main() 函数中创建一个 First 类的实例来访问这些成员。这样做也意味着你可以将 First 的引用传递给 Second,并以这种方式访问这些值。

这些都是棘手的设计决策,如果没有更好地理解什么是 静态,那么在解决这个问题上你可能会遇到困难。

英文:

In main(), you cannot access n, m, and arr because they are non-static. This could be "fixed" by changing them to 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. 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.

In class Second, we can now access n and m by preceding them with their class name, as in First.n and First.m. This is what the static designation allows you to do:

public class Second {

	public double rule;

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

An alternative approach would be to pass both n and m to rules() along with the array. With that approach, however, you would not be able to change n and m and have those changes be reflected back in 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. Doing so would also mean you could pass a reference to First into Second and access the values that way.

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.

huangapple
  • 本文由 发表于 2020年10月25日 20:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64523900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定