简体   繁体   English

将String转换为2D int数组

[英]Convert String into 2D int array

I have problem with conversion from String into two dimension int array. 我从String转换为二维int数组时遇到问题。
Let's say I have: 假设我有:

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

(In my program it will be String from text area.) and I want to create array nxn (在我的程序中,它将是文本区域中的String。),我想创建数组nxn

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

(Necessary for next stages.) I try to split the string and create 1 dimensional array, but I don't have any good idea what to do next. (接下来的步骤是必需的。)我尝试拆分字符串并创建1维数组,但是我不知道下一步要做什么。


As you suggest I try split at first using ; 如您所建议,我首先尝试使用split ; then , but my solution isn't great. 然后,但是我的解决方案不是很好。 It works only when there will be 3 x 3 table. 仅当有3 x 3桌子时,它才起作用。 How to create a loop making String arrays? 如何创建一个使String数组成为循环的循环?

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;
    }
  1. Split by ; 分割; to get rows. 获取行。
  2. Loop them, incrementing a counter (eg x ) 循环它们,增加一个计数器(例如x
    1. Split by , to get values of each row. 除以,以获取每一行的值。
    2. Loop those values, incrementing a counter (eg y ) 循环这些值,增加一个计数器(例如y
      • Parse each value (eg using one of the parseInt methods of Integer ) and add it to the x,y of the array. 解析每个值(例如,使用IntegerparseInt方法之一)并将其添加到数组的x,y中。

If you have already created an int[9] and want to split it into int[3][3] : 如果您已经创建了一个int[9]并想将其拆分为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, ie 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 . 您最好使用Scanner并在nextStringnext之间切换。 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 最大的挑战是,直到到达以行结尾的分号之前,您都不知道每一行中的元素(列)的数量

After using split, take a look at Integer.parseInt() to get the numbers out. 使用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++) {
        cells[i][j] = Integer.parseInt(cells[j]);
    }
}

Then you need to decide what to do with NumberFormatExceptions 然后,您需要决定如何处理NumberFormatExceptions

A solution using 2 splits: 使用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(",");
}

This give a 2 dimension array of strings you will need to parse those ints afterwards, it depends on the use you want for those numbers. 这提供了一个二维数组的字符串,您需要在以后解析这些整数,这取决于您希望这些数字的用途。 The following solution shows how to parse them as you build the result: 以下解决方案显示了在构建结果时如何解析它们:

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]);
    }
}

Super simple method!!! 超级简单的方法!!!

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]);
        }   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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