2014-05-06 64 views
0

我想创建一个新的String [] []数组,但日食给我一个错误:分配双字符串数组

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] = "";} 
+1

是什么错误讯息? – tod

+0

“令牌上的语法错误”;“,{expected before the token” – Tak3r07

+1

初始化值必须定义这种类型的数据结构(数组)因此,应该使用构造函数。 – TeachMeJava

回答

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] = ""; 
    } 
} 
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"; 
        } 
    } 
    
+0

这就是它!谢谢你,我想我以前的方法总是有这些数组,而不是全班同学的全球价值。 – Tak3r07

+0

@ user3248708不客气:) – Mena

+0

@Mena是%100正确:)我在这篇文章的上面说了同样的话。 – TeachMeJava