[英]How to Print a specific point from an array from another method ? #Java
public class NewTest {
public static void main(String[] args)
{
String [][] table;
table = new String [4][4];
for(int y=0; y<4; y++){
for(int x=0; x<4; x++){
table[y][x] = " ~ " ;
}
}
int y, x;
for(y=0; y<4; y++)
{
System.out.print(y+": ");
for(x=0; x<4; x++)
System.out.print(table[y][x]+" ");
System.out.println();
}
}
public void table ()
{
System.out.println(table[2][2]);
}
}
//this is the line where I have problems ! //这是我遇到问题的那一行!
System.out.println(table[2][2]);
The problem is that String [][] table
is local to the method where it is declared, and is, therefore, invisible to other methods of the class. 问题是
String [][] table
对于声明它的方法是本地的,因此对于该类的其他方法是不可见的。
There are two ways of making it visible: 有两种方法可以使其可见:
String [][] table
a static
member in the enclosing class (because main
is static
), or String [][] table
成为封闭类String [][] table
的static
成员(因为main
是static
),或者 String [][] table
to the function as a parameter. String [][] table
作为参数传递给函数。 The second solution is usually better: 第二种解决方案通常更好:
// Here is the declaration of a method taking 2D table
public static void showTableCell(String [][] table) ...
...
// Here is a call of that method from main():
showTableCell(table);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.