线性插值
public static void interpolate(List<Double> list) {
int start = -1;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == null) continue;
if (start != -1) {
int count = i - start;
if (count > 1) {
double delta = (list.get(i) - list.get(start)) / count;
for (int j = start + 1; j < i; j++) {
list.set(j, list.get(j - 1) + delta);
}
}
}
start = i;
}
}
测试
public static void main(String[] args) {
List<Double> list = new ArrayList<>();
list.add(null);
list.add(1d);
list.add(null);
list.add(3d);
list.add(null);
list.add(6d);
list.add(6d);
list.add(null);
list.add(null);
interpolate(list);
for (Double aDouble : list) {
System.out.println(aDouble);
}
}
输出
null
1.0
2.0
3.0
4.5
6.0
7.0
null
null
标签:get,int,线性插值,list,start,add,null
From: https://www.cnblogs.com/windyWu/p/17715475.html