我想创建一个新的String [] []数组,但是eclipse给了我一个错误:
public class CurriculumVitae {
String[][] education = new String[2][6]; //throws error here and expects "{" but why?
education[0][0] = "10/2012 − heute";
education[0][1] = "Studium der Informatik";
education[0][2] = "Johannes Gutenberg−Universit \\”at Mainz";
education[0][3] = "";
education[0][4] = "";
education[0][5] = "";
education[1][0] = "10/2005 − 5/2012";
education[1][1] = "Abitur";
education[1][2] = "Muppet-Gymnasium";
education[1][3] = "Note: 1,3";
education[1][4] = "";
education[1][5] = "";}
答案 0 :(得分:2)
您的声明没问题。
但是,您必须使用初始化程序块来分配array
值。
只需将所有education[x][y]
语句括在花括号中,或将它们移动到构造函数中。
初始化程序块示例
public class CurriculumVitae {
String[][] education = new String[2][6];
// initializer block
{
education[0][0] = "10/2012 − heute";
education[0][1] = "Studium der Informatik";
}
}
构造函数示例
public class CurriculumVitae {
String[][] education = new String[2][6];
// constructor
public CurriculumVitae()
{
education[0][0] = "10/2012 − heute";
education[0][1] = "Studium der Informatik";
}
}
答案 1 :(得分:0)
你的代码必须在方法内。例如:
public class CurriculumVitae {
public static void main(String[] args){
String[][] education = new String[2][6];
education[0][0] = "10/2012 − heute";
education[0][1] = "Studium der Informatik";
education[0][2] = "Johannes Gutenberg−Universit \\”at Mainz";
education[0][3] = "";
education[0][4] = "";
education[0][5] = "";
education[1][0] = "10/2005 − 5/2012";
education[1][1] = "Abitur";
education[1][2] = "Muppet-Gymnasium";
education[1][3] = "Note: 1,3";
education[1][4] = "";
education[1][5] = "";
}
}