Arrays Java基本二维数组 publicstaticvoidmain(字符串[]args){ System.out.println(Planes.p_数组[0][0]); flight_GUI form=新的flight_GUI(); 扫描仪输入=新扫描仪(System.in); 对于(int u=0;u
,代码中的问题是如何尝试访问数组。 如果你有像你这样的代码,你需要做两件事中的一件。你必须自己研究这两件事之间的详细区别,但我会给你一个简短的总结Arrays Java基本二维数组 publicstaticvoidmain(字符串[]args){ System.out.println(Planes.p_数组[0][0]); flight_GUI form=新的flight_GUI(); 扫描仪输入=新扫描仪(System.in); 对于(int u=0;u,arrays,string,Arrays,String,,代码中的问题是如何尝试访问数组。 如果你有像你这样的代码,你需要做两件事中的一件。你必须自己研究这两件事之间的详细区别,但我会给你一个简短的总结 public static void main(String[] args) { System.out.println(Planes.p_array[0][0]); flight_GUI form = new flight_GUI(); Scanner input = new Scanner (System.in); for (int u = 0;
public static void main(String[] args) {
System.out.println(Planes.p_array[0][0]);
flight_GUI form = new flight_GUI();
Scanner input = new Scanner (System.in);
for (int u = 0; u<1;){
for (int x = 0; x<1;){
System.out.println("Would you like to book a flight? yes/no");
String answer = input.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
form.setVisible(true); x=1;
}
else{
System.out.println("Okay!");x=0;
}
}
}}
public class Planes{
public static String[][] p_array = new String [5][5];{
p_array[0][0] = "hello" }}
上面的代码描述了一个非静态类。从某种意义上说,平面只是一个蓝图。它不存在,所以您不能只调用Plane.p\u array
方法1涉及对plane类的实例进行筛选,它将按原样处理plane类:
public class plane {
public String[][] p_array = new String [5][5];{
p_array[0][0] = "3";
}
方法2涉及在plane类中创建一个静态变量,这意味着可以通过您尝试访问它的方式来访问它,但您将看到该变量在该类的所有实例上都是相同的
public static void main(String[] args) {
plane planeObject = new plane(); //create instance of your plane class
System.out.println(planeObject.p_array[0][0]);
//the rest of your code
}
如果您想知道2之间的差异,请查找“静态”和“实例”成员的差异。System.out.println(planes.p_array[0][0])出现了一个错误。planes看起来没有初始化。“非静态变量p_array不能从静态上下文引用”如何以及在何处声明平面?这已经消除了错误,但是现在当它打印出我在[0][0]的数组中的内容时,当我将其设置为“3”(或任何其他内容)时,它打印出“null”。
//the rest of code is the same
public class plane {
public static String[][] p_array = new String [5][5];{
p_array[0][0] = "3";
}