简体   繁体   English

在java中将整数数组划分为不同大小的数组

[英]Dividing an integer array into different sized arrays in java

I have read a .txt file of integers with lines of varying lengths into an array.我已经读取了一个 .txt 整数文件,其中包含不同长度的行到一个数组中。 I now need to subdivide those lines into individual arrays or into a 2d array.我现在需要将这些行细分为单独的数组或二维数组。 The length of the lines of the .txt file are subject to change but the number of lines will not change. .txt 文件的行长可能会发生变化,但行数不会改变。 I am open to suggestions on how to read the file into a jagged array considering the line lengths are subject to change from file to file.考虑到行长度可能会因文件而异,我愿意接受有关如何将文件读入锯齿状数组的建议。

The .txt file looks like this: .txt 文件如下所示:

4 6 4 2 1 6 1 6 1 6 1
7 0 1 2 0 3 0 4 2 3 0 3 2 0 1 2 0 1 7 0 1
1 2 3 4 1 2 5 1 2 3 4 5
1 2 1 3 1 8 1 1 2 5 4 3 4 7 0 7 1 0 1 2 1 2 7 1 2
5 1 3 1 4 5 1 3 5 2 2 3 4 5 4 2 1 4 1 2 3

Here is my code:这是我的代码:

1 import java.io.BufferedReader;
2 import java.io.FileReader;
3 import java.io.IOException;
4 
5 
6 public class FileIn{
7   
8   
9   public static int [] [] PageList = new int [5] [];
10  
11  public static int [] Pages;
12  
13  
14  public static void FileImport() throws IOException {
15      
16              
17      BufferedReader p = new BufferedReader(new FileReader("reference_strings_0010.txt"));
18      
19      String Line;
20              
21      while ((Line = p.readLine()) != null) {
22          
23          Lines.add(Line);
24          String [] IntLine = Line.split(" ");
25          int [] temp = new int [IntLine.length];
26          
27          for (int i = 0; i < temp.length; i++) {
28              
29              temp [i] = Integer.parseInt(IntLine[i]);
30              Pages = new int [temp.length];
31              Pages = temp;
32          }
33          
34      }
35  
36      p.close();
37      
38  }

Currently, the .txt file reads into an array in this format:目前,.txt 文件以这种格式读入一个数组:

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

I would like to have the arrays with each integer being its own element and each array should be separated where the commas are.我希望每个整数都是它自己的元素的数组,并且每个数组应该在逗号所在的位置分开。

Any and all help is greatly appreciated.非常感谢任何和所有帮助。

You should consider using an ArrayList if your final answer will have varying length.如果最终答案的长度不同,则应考虑使用ArrayList Also, you should wrap your parsing in a try-catch block to handle malformed inputs.此外,您应该将解析包装在 try-catch 块中以处理格式错误的输入。 You can store the values in a 2D ArrayList like this:您可以像这样将值存储在 2D ArrayList 中:

 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.IOException;


 public class FileIn{


   public static int [] [] PageList = new int [5] [];

  public static int [] Pages;


  public static void FileImport() throws IOException {


      BufferedReader p = new BufferedReader(new FileReader("reference_strings_0010.txt"));

      String Line;
      ArrayList<ArrayList<Integer>> arr = new ArrayList<>();

      while ((Line = p.readLine()) != null) {

          Lines.add(Line);
          String [] IntLine = Line.split(" ");
          ArrayList<Integer> innerList = new ArrayList<>();

          for (int i = 0; i < IntLine.length; i++) {

              try {
                  int temp = Integer.parseInt(IntLine[i]);
                  innerList.add(temp);
              } catch (NumberFormatException e) {
                  e.printStackTrace();
              }

          }

          arr.add(innerList);
      }

      p.close();

  }

Here, arr will contain your final values in the format:在这里, arr将以以下格式包含您的最终值:

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

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

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