2013-11-25 87 views
0

我想將字符串的一維數組轉換爲二維數組將1D字符串數組轉換爲2D數組中的每個元素

行數等於數組元素數。

所述一個維陣列具有許多元件,從而每個元素應當本身分割爲九個元件之後是對一個行

但產量是除了一個例外維數組 java.lang.ArrayIndexOutOfBoundsException 9

誰能幫助?

public class StringFragementation 

{ 

public static String [][] mymethod(String [] mystring) 
    { 

     int row = 0, col = 0; 

     String[][] india = new String[2][9]; 

     String mystringno2[]; 

     mystringno2 = mystring; 

     int i = 0; 

     int j = 0; 

     String x = "_"; 

     int i1; 

     String Nahda=""; 

     int l; 

     for(l=0;l<mystringno2.length;l++) 

     { 

    Nahda = Nahda.concat(mystringno2[l]); 

     } 

     System.out.println(Nahda); 

     i1 =0; 

    while (j <Nahda.length()) 

    { 

     i = Nahda.indexOf(x, i); 

     i1 = i + 1; 

     i1 = Nahda.indexOf(x, i1); 

    if (i1 >Nahda.length()) 

    { 

     break; 

     } 

     i++; 

    india[row][col] = Nahda.substring(i, i1); 

    System.out.print("Element " + row + " " + col + " " + india[row][col]+"\t"); 

    col++; 

    } 

    return india; 

    } 

public static void main(String[] args) 

{ 

String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"}; 

String [][] singapore = new String[s.length][9]; 

singapore = mymethod(s); 

for(int col=0,k=0;col <9;col++) 

    for(int row=0;row<s.length;row++) 

    { 
    System.out.print( singapore[row][col++]+"\t" ) ; 

    } 

    System.out.println("\n"); 

} 

} 

回答

1

問題是如果找不到字符串,indexOf()返回-1。現在因爲您的國家/地區名稱的字符串最後沒有"_"indexOf函數將返回-1。因此你的循環不會退出,並且col將變成9,這導致IndexOutOfBoundsException

我可能是錯的,但嘗試在最後加上"_"

此外,您從未在循環中增加row,這就是爲什麼返回的數組中只有一個維度。

編輯

好了,現在我明白了,所以你有18個國家,並希望他們與9列安排2行。那麼你永遠不會增加row,這樣就行不通。

試試這個:

public class StringFragementation 

{ 

public static String [][] mymethod(String [] mystring) 
{ 

    int row = 0, col = 0; 

    String[][] india = new String[2][9]; 

    String mystringno2[]; 

    mystringno2 = mystring; 

    int i = 0; 

    int j = 0; 

    String x = "_"; 

    int i1; 

    String Nahda=""; 

    int l; 

    for(l=0;l<mystringno2.length;l++) 

    { 

     Nahda = Nahda.concat(mystringno2[l]); 

    } 

    System.out.println(Nahda); 

    i1 =0; 

    // set i before the loop 
    i = Nahda.indexOf(x, i); 

    while (j <Nahda.length()) { 

     i1 = Nahda.indexOf(x, i + 1); 

     if (i1 == -1) { 
      // No more "_" we're at the end but we still need to add the last country! 
      india[row][col] = Nahda.substring(i, Nahda.length() - 1); 
      break; 
     } 

     india[row][col] = Nahda.substring(i + 1, i1); 

     // set i to the "_" after the current country. 
     i = i1; 

     System.out.print("Element " + row + " " + col + " " + india[row][col].toString()+"\n"); 

     col++; 

     if (col >= 9) { 
      // we're at the end of the first row, go to second row 
      col = 0; 
      row++; 
     } 

    } 

    return india; 

} 

public static void main(String[] args) 

{ 

    String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"}; 

    String [][] singapore = new String[s.length][9]; 

    singapore = mymethod(s); 

    for(int col=0; col < 9;col++) 

     for(int row=0;row<s.length;row++) 

     { 
      System.out.print( singapore[row][col]+"\t" ) ; 

     } 

    System.out.println("\n"); 

} 

} 
+0

我試圖增加在我的循環中的行,但不幸的是;]] – JavaFan

+0

@ElhadiMamoun再次檢查我的答案,我添加了一些代碼 – Octoshape

+0

我只是自己嘗試過,它的工作原理..堅持我會在上面的回答中發佈全班。 – Octoshape

0

我建議你劈到令牌字符串,而不是服用它subSting。對於這種類型的問題,它更像是一種C風格的解決方案。這個怎麼樣?

public static String[][] mymethod(String[] input) { 
     String[][] result = new String[input.length][]; 
     for(int i = 0 ; i < input.length ; i++) { 
      List<String> temp = Arrays.asList(input[i].split("_")); 
      temp = temp.subList(1, temp.size()); 
      String[] row = new String[temp.size()]; 
      temp.toArray(row); 
      result[i] = row; 
     } 
     return result; 
    } 

希望這會有所幫助。

+0

問題是,有一個_在字符串的開頭,我不知道他是否可以改變輸入。 – Octoshape

+0

如果用_分割,第一個_會使第一個生成的標記爲空字符串。使用下面的代碼刪除它:temp = temp.subList(1,temp.size());所以它應該沒問題。 – KKKCoder

+0

哦,沒有看到子列表:)好主意。 – Octoshape

1

這裏是你的代碼的固定版本:

public class StringFragementation 

{ 

public static String [][] mymethod(String [] mystring) 
    { 

     int row = 0, col = 0; 

     String[][] india = new String[2][9]; 

     String mystringno2[]; 

     mystringno2 = mystring; 

     int i = 0; 

     int j = 0; 

     String x = "_"; 

     int i1; 

     String Nahda=""; 

     int l; 

     for(l=0;l<mystringno2.length;l++) 

     { 

    Nahda = Nahda.concat(mystringno2[l]); 

     } 

     System.out.println(Nahda); 

     i1 =0; 

    while (j <Nahda.length()) 

    { 

     i = Nahda.indexOf(x, i); 

     i1 = i + 1; 

     i1 = Nahda.indexOf(x, i1); 

    if (i1 <0) 

    { 

     break; 

     } 

     i++; 

    india[row][col] = Nahda.substring(i, i1); 

    System.out.print("Element " + row + " " + col + " " + india[row][col]+"\n"); 

    col++; 
     if (col > 8) { 
      row++; 
      col=0; 
     } 
    } 

    return india; 

    } 

public static void main(String[] args) 

{ 

String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"}; 

String [][] singapore = new String[s.length][9]; 

singapore = mymethod(s); 

for(int col=0,k=0;col <9;col++) 

    for(int row=0;row<s.length;row++) 

    { 
    System.out.print( singapore[row][col++]+"\t" ) ; 

    } 

    System.out.println("\n"); 

} 

} 

你忘了切換到新的二維陣列(數列的數量,如果它超過8增加列和行重置爲0,你也忘了indexOf返回-1,如果沒有發現,所以我修復,也是

相關問題