简体   繁体   中英

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. 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. 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:

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:

[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. Also, you should wrap your parsing in a try-catch block to handle malformed inputs. You can store the values in a 2D ArrayList like this:

 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:

[ [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] ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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