问题补充说明:我现在两个数组X[i],Y[i] 代表XY轴吧表示如图 两个数组都是double数据类型 然后找他他们的拐点 拐点判断条件如图 将得到的拐点的X[i] Y[i] 赋值给两个新的数组K[i] J[i] 得出新的拐点后 将K[i]的数保留小数点后1位后将重复的K[i]删除 同时赋值给一个新的Knew[i] 同时对应的J[i]也要赋值一个新的 给个核心代码参考下 麻烦各位大神了 想了挺久 一直报错 万分感谢
package com..;
imp360问答ort java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java黄叫守德.util.Random;
public class NikeLoTest {
publi论耐爱战画酸c static void main(String[] args) {
// Cr异层州田群名显查垂到结eate Ran衡dom Array for X and Y
int size = 50;
double[] X = getRandomDoubles(0, 0.4, size);
d验钱号束ouble[] Y = getRandomDoubles(0, 9, size);
/波你盐攻推开药措风华/ pick up value inflection point
List<Double> kList = new ArrayList<>();
List<Double> jList = new ArrayList<>();
for (int i = 0; i < size - 2; i++) { // <- avoid ArrayIndexOutOfBoundsException
if (isInflection(X, Y, i)) {
kList.add(X[i]);
jList.add(Y[i]);
}
}
// K[i] J[i]
Double[] K = kList.toArray(ne似片w Double[kList.size()]);
Double[] J = kList.toArray(new Double[jList.size()]);
// Knew[] Jnew[]
Double[] Knew = roundAndRemoveDuplicateValue(K);
Double[] Jnew = roundAndRemoveDuplicateValue(J);
}
private s杨味总造并你鸡tatic Double[] roundAndRemoveDuplicateValue(Double[] array) {
L促ist<Double> result = new Ar越概创封此负现格rayList<>();
for (Double d : array) {
BigDecimal tmp = new B钱清东igDecima具翻整口衣胞l(d);
tmp = tmp.setScale(1, BigD构商述技图感凯白列得ecimal.R七局河息起OUND_HALF_UP);
i罪引区品f (!result.contains(tmp.doubleValue())) {
result.add(tmp蛋督资回只庆正担省陈功.doubleValue());
}
}
re总turn result.toArray(new Double[result.size()]);
}
p艺识本rivate static boolean isInflection(double[] X, double[] Y, int n) {
return (Y[n + 1] - Y[n]) * (Y[n + 2] - Y[n + 1]) < 0;
}
private static double[] getRandomDoubles(double rangeMin, double rangeMax,
int size) {
double[] array = new double[size];
Random r = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
}
return array;
}
}
标签:Java,算法