我了解ArrayList不能保存任何原始数据
但是如何将我的方法horseFeed()与Arraylist构造函数一起调用到驱动程序中,这样我就不会遇到双重取消引用的错误
也有人可以向我解释什么是双重取消引用的错误以及为什么我得到它,请帮助
该方法在我类
public class horse
{
.
.
.
//took out a lot of code as it was not important to the problem
public String horseFeed(double w)
{
double sFeed= w*.015;
double eFeed= w*.013;
String range = sFeed + " < " + eFeed;
return range;
}
}
这是ArrayList类
import java.util.*;
public class horseStable
{
.
.
.
public double findHorseFeed(int i)
{
double weight = horseList.get(i).getWeight();
return weight;
}
}
这是司机
public class Main
{
public static void main(String args[])
{
//returns the weight of the horse works fine
System.out.println(stable1.findHorseFeed(1));
// This is supposed to use the horseFeed method in the class by using the horse's weight. Where can i place the horseFeed method without getting an error?
System.out.println(stable1.findHorseFeed(1).horseFeed());
}
}
最佳答案
该错误意味着您试图在double
值上调用方法-在Java中,double
是原始类型,您不能在其上调用方法:
stable1.findHorseFeed(1).horseFeed()
^ ^
returns a double can't call any method on it
您需要使用正确的参数在正确的对象上调用该方法,如下所示:
Horse aHorse = new Horse(...);
aHorse.horseFeed(stable1.findHorseFeed(1));
方法
horseFeed()
在Horse
类中,它接收类型为double
的参数,该参数由findHorseFeed()
类中的方法HorseStable
返回。显然,首先您需要创建一个Horse
类型的实例来调用它。另外,请遵循类名以大写字母开头的约定。
关于java - 错误 “double cannot be dereferenced”是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319280/