【问题标题】:Getting a text file into a two dimensional ragged array in Java将文本文件放入Java中的二维不规则数组
【发布时间】:2010-12-16 02:28:38
【问题描述】:

大家好,这是my previous question 的后续活动。我现在有一个格式如下的文本文件:

100 200
123
124 123 145

我想要做的是将这些值放入 Java 中的二维参差不齐的数组中。 我到目前为止是这样的:

public String[][] readFile(String fileName) throws FileNotFoundException, IOException {
       String line = "";
       ArrayList rows = new ArrayList();


       FileReader fr = new FileReader(fileName);
       BufferedReader br = new BufferedReader(fr);

       while((line = br.readLine()) != null) {
        String[] theline = line.split("\\s");//TODO: Here it adds the space between two numbers as an element
        rows.add(theline);
       }
       String[][] data = new String[rows.size()][];
       data = (String[][])rows.toArray(data);
       //In the end I want to return an int[][] this a placeholder for testing
       return data;

我的问题是,例如对于第 100 200 行,变量“theline”具有三个元素 {"100","","200"},然后将其传递给带有 rows.add(theline) 的行 我想要的是只有数字,如果可能的话,如何将这个 String[][] 数组转换为 int[][] 数组 int 最后返回它。 谢谢!

【问题讨论】:

标签: java arrays string file arraylist


【解决方案1】:

您可以尝试使用 StringTokenizer 将行拆分为数字,而不是使用 .split()

【讨论】:

    【解决方案2】:

    如果你使用 Scanner 类,你可以继续调用 nextInt()

    例如(这是 p 代码......你需要清理它)

    scanner = new Scanner(line);
    while(scanner.hasNext())
      list.add(scanner.nextInt())
    row = list.toArray()
    

    这当然也不是很优化。

    【讨论】:

    • 我正在根据您的建议制定解决方案,等我整理好一切后,我会发布它。
    【解决方案3】:

    当我尝试时,您的解析工作正常。 都

     line.split("\\s");
    

     line.split(" ");
    

    将样本数据拆分为正确数量的字符串元素。 (我意识到“\s”版本是更好的方法)

    这是一种将数组转换为 int 数组的蛮力方法

    int [][] intArray = new int[data.length][];
    for (int i = 0; i < intArray.length; i++) {
        int [] rowArray =  new int [data[i].length];
        for (int j = 0; j < rowArray.length; j++) {
            rowArray[j] = Integer.parseInt(data[i][j]);
        }
        intArray[i] = rowArray;
    }
    

    【讨论】:

      【解决方案4】:

      好的,这是基于 Gonzo 建议的解决方案:

      public int[][] readFile(String fileName) throws FileNotFoundException, IOException {
         String line = "";
         ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
      
      
         FileReader fr = new FileReader(fileName);
         BufferedReader br = new BufferedReader(fr);
      
         int r = 0, c = 0;//Read the file
         while((line = br.readLine()) != null) {
             Scanner scanner = new Scanner(line);
             list.add(new ArrayList<Integer>());
             while(scanner.hasNext()){
      
                 list.get(r).add(scanner.nextInt());
                 c++;
             }
             r++;
         }
      
         //Convert the list into an int[][]
         int[][] data = new int[list.size()][];
         for (int i=0;i<list.size();i++){
             data[i] = new int[list.get(i).size()];
            for(int j=0;j<list.get(i).size();j++){
                data[i][j] =  (list.get(i).get(j));
            }
      
      
         }
         return data;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-11-24
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        相关资源
        最近更新 更多