【问题标题】:Casting to generic type and calling doubleValue() method转换为泛型类型并调用 doubleValue() 方法
【发布时间】: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 + '}';
    }
}

【问题讨论】:

    标签: java class generics


    【解决方案1】:

    你可以改变你的

    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 是否具有相同顺序的相同元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多