2013-11-25 88 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,如果没有发现,所以我修复,也是

相关问题