Java 转换为泛型类型并调用doubleValue()方法
我很难让这行代码正常工作:Java 转换为泛型类型并调用doubleValue()方法,java,class,generics,Java,Class,Generics,我很难让这行代码正常工作: 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 {
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 + '}';
}
}
公共类BMICalculatorArrayBag
扩展对象实现可克隆的计算器{
受保护对象[]体重指数;
受保护物体[]高度;
私营企业;
受保护物体[]重量;
公共BMICalculatorArrayBag(){
最终int初始容量=2;
多项目=0;
bmi=新对象[初始容量];
}
公共整数大小(){
返回多个项目;
}
公共int getCapacity(){
返回bmi.length;
}
公共空间容量(int最小容量){
对象[]biggerray;
if(bmi.长度<最小容量){
biggerArray=新对象[最小容量];
系统阵列副本(bmi,0,Biggerray,0,多个项目);
bmi=比格拉雷;
}
}
公共空间trimToSize(){
对象数组;
if(bmi.length!=多个项目){
trimmedArray=新对象[manyItems];
系统阵列副本(bmi,0,trimmedArray,0,多个项目);
bmi=trimmedArray;
}
}
@凌驾
公共空间计算(){
E权重[]=null;
E高度[]=null;
数组.equals(this.height,height);
数组.equals(this.weight,weight);
对于(int i=0;i
您可以更改
protected Object[] bmi;
protected Object[] height;
protected Object[] weight;
进入
由于E扩展了数字
,您仍然可以将所有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);
}
}
不将this.height
复制到height
-它检查this.height
和height
是否具有相同顺序的相同元素
@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);
}
}
Arrays.equals(this.height, height);