【发布时间】:2021-03-17 08:50:49
【问题描述】:
我很难让这行代码工作:
bmi[i] = Math.round((weight[i] / (height[i] * height[i])) * 703);
我将weight[i] 和height[i] 转换为泛型类型E,但是如何在它们上调用doubleValue()?
public class BMICalculatorArrayBag<E extends Number>
extends Object implements Cloneable, Calculator {
protected Object[] bmi;
protected Object[] height;
private int manyItems;
protected Object[] weight;
public BMICalculatorArrayBag() {
final int INITIAL_CAPACITY = 2;
manyItems = 0;
bmi = new Object[INITIAL_CAPACITY];
}
public int size() {
return manyItems;
}
public int getCapacity() {
return bmi.length;
}
public void ensureCapacity(int minimumCapacity) {
Object[] biggerArray;
if (bmi.length < minimumCapacity) {
biggerArray = new Object[minimumCapacity];
System.arraycopy(bmi, 0, biggerArray, 0, manyItems);
bmi = biggerArray;
}
}
public void trimToSize() {
Object[] trimmedArray;
if (bmi.length != manyItems) {
trimmedArray = new Object[manyItems];
System.arraycopy(bmi, 0, trimmedArray, 0, manyItems);
bmi = trimmedArray;
}
}
@Override
public void calculate() {
E weight[] = null;
E height[] = null;
Arrays.equals(this.height, height);
Arrays.equals(this.weight, weight);
for (int i = 0; i < size(); i++) {
bmi[i] = Math.round((weight[i] / (height[i] * height[i])) * 703);
}
}
public void add(E height, E weight) {
if (size() == getCapacity()) {
ensureCapacity(size() * 2 + 1);
}
this.height[size()] = height;
this.weight[size()] = weight;
manyItems++;
calculate();
}
public boolean remove(E targetHeight, E targetWeight) {
int index = 0;
while (index < size() && ((targetHeight != height[index])
|| (targetWeight != weight[index]))) {
index++;
}
if (index == size()) {
return false;
} else {
manyItems--;
height[index] = height[size()];
weight[index] = weight[size()];
bmi[index] = bmi[size()];
return true;
}
}
@Override
public String toString() {
return "BMICalculatorArrayBag{" + "bmi=" + bmi + ", height=" + height + ", manyItems=" + manyItems + ", weight=" + weight + '}';
}
}
【问题讨论】: