我创建了一个数组列表。
List FWD = new ArrayList<Coords>();
FWD.add(new Coords(42.41, 37.23));
FWD.add(new Coords(37.09, 47.8));
FWD.add(new Coords(36.83, 48.42));
然后我要以这种方式访问列表的每个元素:
FWD.get(3).[some method from Coords class]
但是Netbeans说:
找不到符号符号:方法getIndexX()位置:类Object
座谈课:
public class Coords {
private double weightY, indexX;
Coords(double x, double y){
setIndexX(x);
setWeightY(y);
}
public double getWeightY() {
return weightY;
}
public void setWeightY(double weightY) {
this.weightY = weightY;
}
public double getIndexX() {
return indexX;
}
public void setIndexX(double indexX) {
this.indexX = indexX;
}
}
您的变量FWD是类型List
,但实际上包含一个ArrayList<Coords>
,即一个List<Coords>
,但编译器不知道这一点。如果FWD
声明为if,则List<Coords>
此代码应按原样工作。
您还应该注意,Java列表的索引为0,因此列表的第三个元素的索引为2,因此,在您提供的示例中,将不会检索到元素,尽管该元素只会在运行时出现。
最后一点,可能有趣的是注意到Java约定规定了在camelCase中使用的变量名,因此您可能更喜欢fwd
使用变量名或类似名称(如果不太过多,也许使用完整的单词)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句