作为cass member,如何从数组中获取长度



我有一个类,它有一些数组作为成员。在一些成员函数中,我想对所述数组进行迭代。但是当我试图得到数组的长度时,我看到了错误non static variable cannot be referenced from a static context

我有线路for(int i = 0; i < this.blue; i++){},阵列是private int[][] blue;

例如:

public class M{
private int[][] arr;
public static void func(){
for(int i = 0; i < this.arr.length; i++){
//                 ^^^^ compiler does not like this     
}
}
}

我知道我不是在看类的一个实例,所以值可以是任何东西,但是如果我必须事先知道所有东西,我怎么才能预处理这样的基本操作呢?

我曾试图创建另一个拥有维度的成员,但这导致了同样的问题。

public class M{
private int[][] arr;
public void func(){
for(int i = 0; i < this.arr.length; i++){
//                 ^^^^ compiler does not like this     
}
}
}

这将删除错误。这就是从func()中删除静态。在某个时候,我学会了写它,然后不再质疑它。谢谢你在评论中的投入。

最新更新