即使元素类型是Double,编译器也不喜欢将对象中的元素分配给变量类型Double。我想念什么?

Object relWeights [][] = new Object[2][];
relWeights[0] = new String [] {
    "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"
};
relWeights[1] = new Double [] {
    0.78, 0.39, 2.65, 1.17, 1.05, 1.23
};
Double temp = relWeights[1][planetVisiting-1];
//compiler faults here with incompatible types

System.out.println("Your weight on " + relWeights[0][planetVisiting-1] + " is " + temp*weightOnEarth +"pounds");
}

最佳答案

如果出于任何目的将Object类用作类型,然后要将数据转换为其自己的类型。您需要像下面这样投射数据:

Object relWeights [][] = new Object[2][];
relWeights[0] = new String [] {"Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
relWeights[1] = new Double [] {0.78, 0.39, 2.65, 1.17, 1.05, 1.23} ;
Double temp = (Double)relWeights[1][planetVisiting-1];

10-05 22:10