【问题标题】:Dividing an integer array into different sized arrays in java在java中将整数数组划分为不同大小的数组
【发布时间】:2019-12-14 07:34:49
【问题描述】:

我已将一个包含不同长度行的整数的 .txt 文件读入一个数组。我现在需要将这些行细分为单独的数组或二维数组。 .txt 文件的行长可能会发生变化,但行数不会改变。考虑到行长可能因文件而异,我愿意接受有关如何将文件读入锯齿状数组的建议。

.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

这是我的代码:

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  }

目前,.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]

我想让数组中的每个整数都是它自己的元素,并且每个数组都应该在逗号所在的地方分开。

非常感谢任何和所有帮助。

【问题讨论】:

  • Xell,将每个 1D 数组 temp 存储在 2D 数组 PageList 中。请尊重 Java 命名约定。删除没有任何用途的页面。

标签: java arrays arraylist integer jagged-arrays


【解决方案1】:

如果您的最终答案长度不同,您应该考虑使用ArrayList。此外,您应该将解析包装在 try-catch 块中以处理格式错误的输入。您可以像这样将值存储在 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();

  }

在这里,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] ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多