代码之家  ›  专栏  ›  技术社区  ›  A0sXc950

如何从文件(Java)向2d数组中添加double?

  •  2
  • A0sXc950  · 技术社区  · 7 年前

    我看到的所有示例都涉及在文件开头指定行数和列数,但我正在使用的方法读取具有以下内容的文件:

    1.0 2.0
    3.0 4.0
    

    这是我写的代码:

     public static double[][] readMatrixFrom(String file) throws FileNotFoundException {
         Scanner input = new Scanner(new FileReader(file));
         int rows =0;
         int columns =0;
    
         while(input.hasNextLine()){
             String line = input.nextLine();
             rows++;
             columns = line.length();     
         }
         double[][] d = new double[rows][columns]
         return d;      
    }
    

    我不确定如何添加这些值,现在我已经创建了二维数组。我试过了但得到了一个 InputMismatchException .

    Scanner s1 = new Scanner(file);
    double[][] d = new double[rows][columns]
    
    for (int i= 0;i<rows;i++) {
        for (int j= 0;i<rows;j++) {
             d[i][j] = s1.nextDouble();
         }
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Elliott Frisch    7 年前

    为了使用未知大小的数组,您应该将数据读入 Collection (例如 List ). 然而, 收藏 (s) 只处理包装器类型;因此需要将元素复制回 double (s) 如果这就是你 需要。有点像,

    public static double[][] readMatrixFrom(String file) throws FileNotFoundException {
        Scanner input = new Scanner(new FileReader(file));
        List<List<Double>> al = new ArrayList<>();
        while (input.hasNextLine()) {
            String line = input.nextLine();
            List<Double> ll = new ArrayList<>();
            Scanner sc = new Scanner(line);
            while (sc.hasNextDouble()) {
                ll.add(sc.nextDouble());
            }
            al.add(ll);
        }
        double[][] d = new double[al.size()][];
        for (int i = 0; i < al.size(); i++) {
            List<Double> list = al.get(i);
            d[i] = new double[list.size()];
            for (int j = 0; j < d[i].length; j++) {
                d[i][j] = list.get(j);
            }
        }
        return d;
    }
    

    我通过在我的主文件夹中创建一个包含您的内容的文件并像这样运行来测试它

    public static void main(String[] args) {
        String file = System.getProperty("user.home") + File.separator + "temp.txt";
        try {
            System.out.println(Arrays.deepToString(readMatrixFrom(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    

    我得到了(我想你想要的)

    [[1.0, 2.0], [3.0, 4.0]]
    
        2
  •  2
  •   Aman J    7 年前

         Scanner input = new Scanner(new FileReader(file));
    
         int row=0;
         int col =0;
         String s="";
    
         //count number of rows
         while(input.hasNextLine()) {
             row++;
             s=input.nextLine();
         }
    
         //count number of columns
         for(char c: s.toCharArray()) {
             if(c==' ')
                 col++;
         }
    
         col++; // since columns is one greater than the number of spaces
    
         //close the file
         input.close();
    
         // and open it again to start reading it from the begining
         input = new Scanner(new FileReader(file));
         //declare a new array
         double[][] d = new double[row][col];   
    
         int rowNum=0;
         while(input.hasNextLine()) {
             for(int i=0; i< col; i++) {
                 d[rowNum][i]= input.nextDouble();
             }
             rowNum++;
         }
    

    但是,如果您喜欢使用java集合,则可以避免再次读取该文件。只需将字符串存储在列表中,并在列表上迭代以从中提取元素。

        3
  •  2
  •   Praveen    7 年前

    根据您的输入,您的 columns = line.length(); 正在返回 7个 2个 String 长度。

    columns = line.split(" ").length;

    同时,当你试图阅读你的输入时,你正在使用索引 i 第二次 for-loop

    for (int i= 0;i<rows;i++) {
        for (int j= 0;j<columns;j++) {
             d[i][j] = s1.nextDouble();
         }
    }
    
    推荐文章