Java 将一维字符串数组转换为二维数组行中的每个元素

Java 将一维字符串数组转换为二维数组行中的每个元素,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我想把一维字符串数组转换成二维数组 行数等于数组元素数 一维数组有很多元素,所以每个元素都应该在一行上 把自己分成九行之后 但除了异常之外,输出是一维数组 java.lang.ArrayIndexOutOfBoundsException 9 有人能帮忙吗 public class StringFragementation { public static String [][] mymethod(String [] mystring) { int row = 0

我想把一维字符串数组转换成二维数组

行数等于数组元素数

一维数组有很多元素,所以每个元素都应该在一行上

把自己分成九行之后

但除了异常之外,输出是一维数组 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");

}

}
公共类StringFragementation
{
公共静态字符串[][]mymethod(字符串[]mystring)
{
int行=0,列=0;
字符串[][]印度=新字符串[2][9];
字符串mystringno2[];
mystringno2=mystring;
int i=0;
int j=0;
字符串x=“389;”;
int i1;
字符串Nahda=“”;
int l;

对于(l=0;l而言,问题是如果找不到字符串,
indexOf()
将返回-1。现在,因为具有国家名称的字符串没有
“\u0”
在最后,
indexOf
函数将返回-1。因此,循环将不会退出,
col
将变为9,从而导致
IndexOutOfBoundsException

我可能错了,但请尝试在末尾添加一个
“\u”

此外,在循环中从不增加
,因此在返回的数组中只有一个维度

编辑

好的,现在我明白了,你有18个国家,你想把它们排列成2行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;

    // 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");

}

}
公共类StringFragementation
{
公共静态字符串[][]mymethod(字符串[]mystring)
{
int行=0,列=0;
字符串[][]印度=新字符串[2][9];
字符串mystringno2[];
mystringno2=mystring;
int i=0;
int j=0;
字符串x=“389;”;
int i1;
字符串Nahda=“”;
int l;

对于(l=0;l我建议您将字符串拆分为标记,而不是对其进行替换。这更像是此类问题的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;
    }
publicstaticstring[]mymethod(String[]input){
字符串[][]结果=新字符串[input.length][];
for(int i=0;i

希望这有帮助。

以下是您的代码的固定版本:

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");

}

}
公共类StringFragementation
{
公共静态字符串[][]mymethod(字符串[]mystring)
{
int行=0,列=0;
字符串[][]印度=新字符串[2][9];
字符串mystringno2[];
mystringno2=mystring;
int i=0;
int j=0;
字符串x=“389;”;
int i1;
字符串Nahda=“”;
int l;

对于(l=0;lI在我的循环中尝试了增加行,但不幸的是没有]]我自己也尝试过,它有效..等等,我将在上面的答案中发布整个类。@ElhadiMamoun所以你删除了你的评论,它现在有效吗?它有效,但为什么不打印元素,为什么它会出现异常?[代码]_荷兰、冰岛、挪威、丹麦、美国、巴西、阿根廷、哥伦比亚、玻利维亚、比利时、罗马尼亚、法国、德国、瑞士、俄罗斯、瑞典、意大利、奥地利线程“main”java.lang.ArrayinDexOutofBounds异常:StringFragementation.mymethod(StringFragementation.java:55)在StringFragementation.main(StringFragementation.java:80)中元素0 0元素0 1元素0 2元素0 3元素0 4元素0 5元素0 6元素0 7元素0 8元素1 0。。。。。。。。etc@ElhadiMamoun是的,我发现了错误。再次检查我的答案。我发布了整个课程,现在可以了!:)问题是字符串的开头已经有一个u,我不确定他是否可以更改输入。如果按u分割,第一个u将导致第一个生成的标记为空字符串。它在上面使用以下行删除:temp=temp.subList(1,temp.size());所以应该可以。哦,没有看到子列表:)好主意。