繁体   English   中英

将1D String数组转换为2D数组中的每个元素

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM