2014-05-06 63 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