【问题标题】:How to pass parseInt array to previous array?如何将 parseInt 数组传递给前一个数组?
【发布时间】:2018-07-12 14:41:56
【问题描述】:

我被要求读取一个文件并将其中的文本转换为二维数组。由于 Eclipse 很痛苦并且不会打开/读取我的文本文件,因此我在使用单独初始化的 2d 数组的类中进行了测试。我的问题是我不知道将 parseInt'ed 数组放回新数组中。或者如何使用它来形成一个新的二维数组。这是我的代码: public static void main(String[] 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"));
}
}

我想生成一个如下所示的数组:

-1 2 3 0

-1 3 4 0

-1 3 -4 0

-1 -3 4 0

我怎样才能做到这一点?

【问题讨论】:

  • 嗨,这样做formula[count] = line;
  • 天哪,我好密集...我不敢相信我错过了这个...谢谢!!!!

标签: java arrays algorithm multidimensional-array string-parsing


【解决方案1】:

像这样改变formula的定义:

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

您希望公式与测试程序具有相同的行数,对吗?

还将您的 while 循环更改为循环直到 tester.length 而不是常量 4:

while (counter < tester.length)

现在,在for 循环之后才是真正的业务开始的地方:

for (int i = 0; i < charArray.length; i++) {
    String numAsStr = charArray[i];
    line[i] = Integer.parseInt(numAsStr);
}
formula[row] = line; // <------------

在 for 循环期间,您已经解析了测试器一行中的所有整数。现在是时候将整数行放入formula,不是吗?

【讨论】:

  • 谢谢你,尤其是循环和宽度条件!
【解决方案2】:

您必须将项目添加到数组formula。只需在此处添加formula[count] = line;

for (int i = 0; i < charArray.length; i++) {
        String numAsStr = charArray[i];
        line[i] = Integer.parseInt(numAsStr);
        formula[count] = line;
    }

输出是:

[[-1, 2, 3, 0]
[-1, 3, 4, 0]
[-1, 3, -4, 0]
[-1, -3, 4, 0]]

如果有效,请选择答案!

【讨论】:

    【解决方案3】:

    How to read a large text file line by line using Java? Java file to 2D array

    -1 2 3 0
    -1 3 4 0
    -1 3 -4 0
    -1 -3 4 0
    

    演示

    import java.io.File;
    import java.util.Arrays;
    import java.util.Scanner;
    import java.io.IOException;
    
    public class MyFile {
        public static void main(String[] args) throws IOException {
    
            File file = new File("myFile.txt"); // read file name
            Scanner scan = null;
    
            int[][] myArray = new int[4][4]; // how many?
            String line = null; // line in .txt
            String[] token = null; // number token
    
            try {
                scan = new Scanner(file);
                while (scan.hasNextLine()) {
                    for (int i = 0; i < 4; i++) { // loop thru row
                        line = scan.nextLine(); // scan line
                        token = line.split(" ");
    
                        for (int j = 0; j < 4; j++) {
                            myArray[i][j] = Integer.parseInt(token[j]);// loop thru col & parse token int
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            scan.close();
            System.out.println(Arrays.deepToString(myArray)); //fix this
        }
    }
    

    【讨论】:

      【解决方案4】:

      我建议您使用可以存储数组的 List,然后它可以轻松转换为 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();
              });
      
          }
      
      }
      

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

      【讨论】:

        【解决方案5】:

        添加formula[row] = line; for循环

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-15
          • 1970-01-01
          • 2018-10-19
          • 2011-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多