我想將字符串的一維數組轉換爲二維數組將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