繁体   English   中英

转换为泛型类型并调用 doubleValue() 方法

[英]Casting to generic type and calling doubleValue() method

我很难让这行代码工作:

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 + '}';
    }
}

你可以改变你的

protected Object[] bmi;
protected Object[] height;
protected Object[] weight;

进入

protected Double[] bmi;
protected Number[] height;
protected Number[] weight;

由于E extends Number ,您仍然可以将所有E值存储到身高和体重中。

然后你的计算方法会变成

@Override
public void calculate() {
    for (int i = 0; i < size(); i++) {
        bmi[i] = Math.round((weight[i].doubleValue() / (height[i].doubleValue() * height[i].doubleValue())) * 703);
    }
}

请注意,我明确地将bmi数组更改为Double数组 - bmi 计算的结果将始终是双精度数。


附带说明:

Arrays.equals(this.height, height);

不会将this.height复制到height - 它检查this.heightheight是否具有相同顺序的相同元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM