[英]Convert 1D String array to 2D array each element in row
我想將一維字符串數組轉換為二維數組
行數等於數組元素的數量。
一維數組有許多元素,因此每個元素應該在一行上
在將自己分成九個元素之后。
但是除了異常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");
}
}
問題是如果找不到字符串, indexOf()
將返回-1。 現在因為帶有國家名稱的字符串在最后沒有"_"
, indexOf
函數將返回-1。 因此,你的循環不會退出, col
將變為9,這會導致IndexOutOfBoundsException
。
我可能錯了,但最后在最后添加一個"_"
。
你也永遠不會在你的循環中增加row
,這就是為什么返回的數組中只有一個維度。
編輯
好的,現在我明白了,所以你有18個國家,並希望將它們安排在2行9列。 那么你永遠不會增加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");
}
}
以下是代碼的固定版本:
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");
}
}
你忘了切換到新的2D數組(計算列數,如果它超過8,增加列並將行重置為0,你也忘了indexOf如果沒有找到則返回-1,所以我也修復了
我建議你將String拆分為令牌,而不是使用它的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;
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.