【发布时间】:2018-12-05 14:07:01
【问题描述】:
我有两个数据文件 data1.txt 和 data2.txt。 data1.txt 由 3 列和 10 行组成,data2.txt 由 4 列和 4 行组成。
data1.txt的结构是
1.0 1.235 2.3145
2.0 0.325 3.2145
3.0 1.568 4.0215
4.0 0.389 1.2354
7.0 2.054 1.3247
8.0 1.111 2.6570
10.0 1.786 1.2587
11.0 5.628 0.2354
13.0 4.897 1.6542
15.0 1.230 1.0210
data2.txt的结构是
5.0 .... .... ....
6.0 .... .... ....
9.0 .... .... ....
12.0 .... .... ....
因此,根据 data2.txt 中的 data1.txt 中的数据集 5.0,6.0 缺失。 data2.txt 中存在的 data1.txt 中缺少 9.0,依此类推。我想在 data1.txt 中插入这些缺失值。所以插入后数据文件的输出会是
Data1.txt
1.0 1.235 2.3145
2.0 0.325 3.2145
3.0 1.568 4.0215
4.0 0.389 1.2354
5.0 .... .... ....
6.0 .... .... ....
7.0 2.054 1.3247
8.0 1.111 2.6570
9.0 .... .... ....
10.0 1.786 1.2587
11.0 5.628 0.2354
and so on
我的代码是这样的
public class F1 {
public static void main(String args[])throws Exception{
Scanner X =new Scanner(new File("C:\\data1.txt"));
Scanner Y =new Scanner(new File("C:\\data2.txt"));
double a=0.0,b=0.0,c,d=0.0,e=0.0,f,g,h;
List<Double>list1=new ArrayList<>();
List<Double>list4=new ArrayList<>();
double arr[]=new double[10];
double arr1[]=new double[5];
while (X.hasNext()) {
a = X.nextDouble();
list1.add(a);
b = X.nextDouble();
c = X.nextDouble();
}
while(Y.hasNext()) {
d = Y.nextDouble();
list4.add(d);
e = Y.nextDouble();
f = Y.nextDouble();
g = Y.nextDouble();
}
for(int i=0;i<list1.size();i++) {
arr[i]=list1.get(i);//Store value into an array
}
for(int j=0;j<list4.size();j++){
arr1[j]=list4.get(j);//Store value into an array
}
for(int k=0;k<arr.length;k++){
// Appending condition
}
}
}
我该如何继续?是否需要任何插入排序或任何 if 语句来追加? Printwriter 是如何在这里工作的?
【问题讨论】:
标签: java arrays file java.util.scanner filewriter