如何从文件 (Java) 中将双精度值添加到二维数组中?

How to add doubles into a 2d array from a file (Java)?

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

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();
     }
}

如果你只想使用基本数组,你可以用类似

的东西来实现
     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 集合,则可以避免再次读取该文件。只需将字符串存储在列表中并遍历列表以从中提取元素。

为了使用未知大小的数组,您应该将数据读入 Collection(例如 List)。但是,Collection(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]]

根据您的输入,您的 columns = line.length(); 返回 7 而不是 2 因为它 returns String长度。

因此尝试计算行中的列数 columns = line.split(" ").length;

此外,在尝试读取您的输入时,您在第二个 for-loop 中使用了索引 i。应该像下面这样,

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