我想将字符串的一维数组转换为二维数组将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");
}
}
我试图增加在我的循环中的行,但不幸的是;]] – JavaFan
@ElhadiMamoun再次检查我的答案,我添加了一些代码 – Octoshape
我只是自己尝试过,它的工作原理..坚持我会在上面的回答中发布全班。 – Octoshape