1

我在从 String 转换为二维 int 数组时遇到问题。
假设我有:

String x = "1,2,3;4,5,6;7,8,9"

(在我的程序中,它将是来自文本区域的字符串。)我想创建数组n x n

int[3][3] y = {{1,2,3},{4,5,6},{7,8,9}} 

(下一阶段所必需的。)我尝试拆分字符串并创建一维数组,但我不知道下一步该做什么。


正如您建议的那样,我首先尝试使用;then 进行拆分,,但我的解决方案不是很好。它仅在有3 x 3桌子时才有效。如何创建一个循环制作字符串数组?

public int[][] RunMSTFromTextFile(JTextArea ta)
    {
        String p = ta.getText();
        String[] tp = p.split(";");

        String tpA[] = tp[0].split(",");
        String tpB[] = tp[1].split(",");
        String tpC[] = tp[2].split(",");

        String tpD[][] = {tpA, tpB, tpC};

        int matrix[][] = new int[tpD.length][tpD.length];

        for(int i=0;i<tpD.length;i++)
        {
            for(int j=0;j<tpD.length;j++)
            {
                matrix[i][j] = Integer.parseInt(tpD[i][j]);
            }
        }
        return matrix;
    }
4

5 回答 5

3
  1. 拆分;以获取行。
  2. 循环它们,增加一个计数器(例如x
    1. 拆分,以获取每行的值。
    2. 循环这些值,增加一个计数器(例如y
      • 解析每个值(例如使用 的parseInt方法之一Integer)并将其添加到x,y数组中。
于 2013-01-24T23:00:17.817 回答
2

If you have already created an int[9] and want to split it into int[3][3]:

for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
    toArray[i][j] = fromArray[(3*i) + j);
  }
}

Now, if the 2-dimensional array is not rectangular, i.e. the size of inner array is not same for all outer arrays, then you need more work. You would do best to use a Scanner and switch between nextString and next. The biggest challenge will be that you will not know the number of elements (columns) in each row until you reach the row-terminating semi-colon

于 2013-01-24T22:50:50.193 回答
2

使用 split 后,看一下Integer.parseInt()以获取数字。

String lines[] = input.split(";");
int width = lines.length;
String cells[] = lines[0].split(",");
int height = cells.length;
int output[][] = new int[width][height];

for (int i=0; i<width; i++) {
    String cells[] = lines[i].split(",");
    for(int j=0; j<height; j++) {
        output[i][j] = Integer.parseInt(cells[j]);
    }
}

然后你需要决定如何处理NumberFormatExceptions

于 2013-01-24T23:00:10.360 回答
0

使用 2 个拆分的解决方案:

String input = "1,2,3;4,5,6;7,8,9";

String[] x = input.split(";");

String[][] result = new String[x.length][];
for (int i = 0; i<x.length; i++) {
    result[i] = x[i].split(",");
}

这给出了一个二维字符串数组,您需要在之后解析这些整数,这取决于您对这些数字的用途。以下解决方案显示了如何在构建结果时解析它们:

String input = "1,2,3;4,5,6;7,8,9";

String[] x = input.split(";");

int[][] result = new int[x.length][];

for (int i = 0; i < x.length; i++) {
    String[] row = x[i].split(",");
    result[i] = new int[row.length];

    for(int j=0; j < row.length; j++) {
        result[i][j] = Integer.parseInt(row[j]);
    }
}
于 2013-01-24T23:02:41.353 回答
0

超级简单的方法!!!

package ADVANCED;
import java.util.Arrays;
import java.util.Scanner;

public class p9 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);

        String x=sc.nextLine();
        String[] array = x.split(",");
        int length_x=array.length;
        int[][] two=new int[length_x/2][2];

        for (int i = 0; i <= length_x-1; i=i+2) {
            two[i/2][0] = Integer.parseInt(array[i]);
        }

        for (int i = 1; i <= length_x-1; i=i+2) {
            two[i/2][1] = Integer.parseInt(array[i]);
        }   
    }
}
于 2017-10-23T05:30:00.073 回答