Java 如何将parseInt数组传递给上一个数组?

Java 如何将parseInt数组传递给上一个数组?,java,arrays,algorithm,multidimensional-array,string-parsing,Java,Arrays,Algorithm,Multidimensional Array,String Parsing,我被要求读取一个文件,将其中的文本转换成二维数组。因为Eclipse是一个痛苦的东西,而且不会打开/读取我的文本文件,所以我在使用单独初始化的2d数组的类中做了一个测试。我的问题是,我不知道如何将解析后的数组放回新数组中。或者如何使用它来形成一个新的二维阵列。这是我的密码: 公共静态无效字符串[]args{ String[][] tester = new String[][] { { "-1 2 3 0" }, { "-1 3 4 0" }, { "-1 3 -4 0" }, { "-1

我被要求读取一个文件,将其中的文本转换成二维数组。因为Eclipse是一个痛苦的东西,而且不会打开/读取我的文本文件,所以我在使用单独初始化的2d数组的类中做了一个测试。我的问题是,我不知道如何将解析后的数组放回新数组中。或者如何使用它来形成一个新的二维阵列。这是我的密码: 公共静态无效字符串[]args{

    String[][] tester = new String[][] { { "-1 2 3 0" }, { "-1 3 4 0" }, { "-1 3 -4 0" }, { "-1 -3 4 0" } };

    int row = 0;
    int col = 0;
    int count = 0;
    int[][] formula = new int[4][];

    while (count < 4) {
        String temp = tester[row][col];
        String[] charArray = temp.split("\\s+");
        int[] line = new int[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            String numAsStr = charArray[i];
            line[i] = Integer.parseInt(numAsStr);
            //what to do here??
        }

        row++;
        count++;

    }

    System.out.println(Arrays.deepToString(formula).replace("], ",
     "]\n"));
}
}
我想生成一个如下所示的数组:

int[][] formula = new int[tester.length][];
-1230

-1430

-1 3-4 0

-1-34 0


如何完成此操作?

您必须向数组公式中添加项。只需添加公式[count]=line;此处:


如果有效,请选择作为答案!

您必须将项目添加到数组公式中。只需添加公式[count]=line;此处:

如果有效,请选择作为答案!

更改公式的定义如下:

int[][] formula = new int[tester.length][];
您希望公式的行数与测试仪的行数相同,对吗

也可以将while循环更改为循环,直到tester.length,而不是常量4:

while (counter < tester.length)
在for循环期间,您已经解析了测试仪一行中的所有整数。现在是时候将这行整数放入公式中了,不是吗?

更改公式的定义如下:

int[][] formula = new int[tester.length][];
您希望公式的行数与测试仪的行数相同,对吗

也可以将while循环更改为循环,直到tester.length,而不是常量4:

while (counter < tester.length)
在for循环期间,您已经解析了测试仪一行中的所有整数。现在是时候将这行整数放入公式中了,不是吗?

演示

演示


在for循环下添加公式[行]=行;在for循环下添加公式[行]=行;在for循环下,我建议您使用一个列表来存储数组,然后将其轻松转换为int[][]

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        String test = "-1 2 3 0\n-1 3 4 0\n-1 3 -4 0\n-1 -3 4 0";

        Scanner sc = new Scanner(test);

        //read
        List<int[]> arrayList = new ArrayList<>();

        while (sc.hasNextLine()) {
            arrayList.add(Arrays.stream(sc.nextLine().split(" "))
                    .mapToInt(Integer::parseInt)
                    .toArray());
        }

        //print
        arrayList.forEach(arr -> {
            Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
            System.out.println();
        });

        //convert
        int[][] matrix = new int[arrayList.size()][];
        matrix = arrayList.toArray(matrix);


        //print
        Arrays.stream(matrix).forEach(arr -> {
            Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
            System.out.println();
        });

    }

}

请注意,如果您必须在多个正则表达式模式上拆分它,则必须为每一行使用一个平面图。

我建议您使用一个列表,在其中可以存储数组,然后可以轻松地将其转换为int[][]

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        String test = "-1 2 3 0\n-1 3 4 0\n-1 3 -4 0\n-1 -3 4 0";

        Scanner sc = new Scanner(test);

        //read
        List<int[]> arrayList = new ArrayList<>();

        while (sc.hasNextLine()) {
            arrayList.add(Arrays.stream(sc.nextLine().split(" "))
                    .mapToInt(Integer::parseInt)
                    .toArray());
        }

        //print
        arrayList.forEach(arr -> {
            Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
            System.out.println();
        });

        //convert
        int[][] matrix = new int[arrayList.size()][];
        matrix = arrayList.toArray(matrix);


        //print
        Arrays.stream(matrix).forEach(arr -> {
            Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
            System.out.println();
        });

    }

}

请注意这样一个事实:如果您必须将其拆分为多个正则表达式模式,则必须为每一行使用一个平面图。

hi,只需执行此公式[count]=行;天哪,我太密集了…我简直不敢相信我错过了此公式…谢谢!!!!嗨,执行此公式[count]=线;天哪,我太密集了…我真不敢相信我错过了这个…谢谢!!!!谢谢你,特别是循环和宽度条件!谢谢你,特别是循环和宽度条件!
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        String test = "-1 2 3 0\n-1 3 4 0\n-1 3 -4 0\n-1 -3 4 0";

        Scanner sc = new Scanner(test);

        //read
        List<int[]> arrayList = new ArrayList<>();

        while (sc.hasNextLine()) {
            arrayList.add(Arrays.stream(sc.nextLine().split(" "))
                    .mapToInt(Integer::parseInt)
                    .toArray());
        }

        //print
        arrayList.forEach(arr -> {
            Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
            System.out.println();
        });

        //convert
        int[][] matrix = new int[arrayList.size()][];
        matrix = arrayList.toArray(matrix);


        //print
        Arrays.stream(matrix).forEach(arr -> {
            Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
            System.out.println();
        });

    }

}