[英]How to add doubles into a 2d array from a file (Java)?
All the examples that i have seen involve specifying the number of rows and columns at the start of the file but the method I'm working on reads a file with the following: 我看到的所有示例都涉及在文件开头指定行和列的数量,但是我正在使用的方法使用以下内容读取文件:
1.0 2.0
3.0 4.0
and using this data creates a 2d array and stores it without specifying the number of rows and columns. 并使用此数据创建一个二维数组并存储它,而无需指定行数和列数。
Here's the code I have written: 这是我编写的代码:
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;
}
I'm unsure of how to add these values now that I have created the 2d array. 创建2d数组后,我不确定如何添加这些值。 i tried this but got an
InputMismatchException
. 我尝试了这个但是得到了
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();
}
}
if you just want to use the basic arrays you can achieve it with something like 如果您只想使用基本数组,则可以通过以下方式实现
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++;
}
However if you prefer to use java collection you can avoid reading the file again. 但是,如果您更喜欢使用Java集合,则可以避免再次读取文件。 Just store the strings in a list and iterate over the list to extract elements from it.
只需将字符串存储在列表中,然后遍历列表以从中提取元素。
Based on your input, Your columns = line.length();
根据您的输入,您的
columns = line.length();
is returning 7 rather than 2 as it returns the String
length. 返回7而不是2,因为它返回
String
长度。
Hence try calculating the no of columns in the row columns = line.split(" ").length;
因此,请尝试计算行中的列
columns = line.split(" ").length;
Also while trying to read your input you were using index i
for the 2nd for-loop
. 同样,在尝试读取输入时,您将索引
i
用于第二个for-loop
。 It should be like below, 应该像下面这样
for (int i= 0;i<rows;i++) {
for (int j= 0;j<columns;j++) {
d[i][j] = s1.nextDouble();
}
}
In order to work with arrays of unknown size you should read the data into a Collection
(such as a List
). 为了使用未知大小的数组,您应该将数据读入
Collection
(例如List
)。 However, Collection
(s) only work with the wrapper-types; 但是,
Collection
仅适用于包装器类型; so you will need to copy the elements back into an array of double
(s) if that is what you really need. 因此,如果您确实需要,则需要将元素复制回
double
精度数组。 Something like, 就像是,
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;
}
Which I tested by creating a file in my home folder with your contents and running it like so 我通过在主文件夹中创建一个包含您的内容的文件并像这样运行该文件进行了测试
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();
}
}
And I get (as I assume you wanted) 我得到了(就像我想的那样)
[[1.0, 2.0], [3.0, 4.0]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.