简体   繁体   English

(Java)将字符串解析为2D整数数组

[英](Java) Parsing a String into a 2D Array of Integers

It's my first time posting here, so please be gentle. 这是我第一次在这里发布信息,请保持温柔。

I've been given a plain text file that has an accumulation of numbers on different lines. 我得到了一个纯文本文件,该文件在不同的行上有大量的数字。 (10x10 grid) (10x10格)

0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789

I've managed to load it into my Java application and print it out using.. 我设法将其加载到我的Java应用程序中并使用打印出来。

System.out.println(stringBuffer.toString());

This prints out the file to the command line with no issues. 这样就可以毫无问题地将文件输出到命令行。

But I can't seem to parse it through as a integer in a 2D array? 但是我似乎无法将其解析为2D数组中的整数?

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9....]

How can I achieve this? 我该如何实现?

Thank you. 谢谢。

try 尝试

import java.util.Arrays;
public class test {
    public static void main(String[] args) throws FileNotFoundException {
        StringBuffer stringBuffer = new StringBuffer("0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789\n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789\n0123456789\n0123456789");
        String str[]= stringBuffer.toString().split("\n");
        int[][] arr = new int[str.length][];

        for(int i=0;i<str.length;i++){
            String currentLine=str[i].trim();
            int[] temp = new int[currentLine.length()];         
            for(int j=0;j<currentLine.length();j++){
                temp[j] =  Character.getNumericValue(currentLine.charAt(j));
            }
            arr[i]=temp;

        }
        System.out.println(Arrays.deepToString(arr));
    }

}

Use String.toCharArray() method: 使用String.toCharArray()方法:

// after reading a line into 'numString'

char [] charList = numString.toCharArray();
int [] intList = new int [charList.length];

for(int i = 0; i < intList.length; i++) {
    intList[i] = charList[i] - '0';

// now you have the ints from the line into intList
public int[][] to2D(String value) {
    String[] linesArray = value.split("\n");
    int[][] array2d = new int[linesArray.length][];
    for (int i = 0; i < linesArray.length; i++) {
        char[] lineArray = linesArray[i].toCharArray();
        int[] array1d = new int[lineArray.length];
        for (int j = 0; j < lineArray.length; j++) {
            array1d[j] = Character.getNumericValue(lineArray[j]);
        }
        array2d[i] = array1d; 
    }   
    return array2d;
}

I prefer to work with Collections though (ArrayList in that case): 我更喜欢使用Collections(在这种情况下为ArrayList):

public ArrayList<ArrayList<Integer>> to2DList(String value) {
    ArrayList<ArrayList<Integer>> arrayList2d = new ArrayList<ArrayList<Integer>>();
    String[] lines = value.split("\n");
    for (String line : lines) {
        ArrayList<Integer> arrayList1d = new ArrayList<Integer>();
        for (char digit : line.toCharArray()) {
            arrayList1d.add(Character.getNumericValue(digit));
        }
        arrayList2d.add(arrayList1d);
    }   
    return arrayList2d;
}

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

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