java:<标识符>预期使用 ArrayList标识符>
我有一个名为存储的类。存储包含一个称为“产品”的特殊对象的数组列表。每个产品都包含名称、价格等信息。我的代码如下:
class Storage{
Product sprite = new Product("sprite",1.25,30);
Product pepsi = new Product("pepsi",1.85,45);
Product orange = new Product("orange",2.25,36);
Product hershey = new Product("hershey",1.50,33);
Product brownie = new Product("brownie",2.30,41);
Product apple = new Product("apple",2.00,15);
Product crackers = new Product("peanut",3.90,68);
Product trailmix = new Product("trailmix",1.90,45);
Product icecream = new Product("icecream",1.65,28);
Product doughnut = new Product("doughnut",2.75,18);
Product banana = new Product("banana",1.25,32);
Product coffee = new Product("coffee",1.30,40);
Product chips = new Product("chips",1.70,35);
ArrayList<Product> arl = new ArrayList<Product>();
//add initial elements to arraylist
arl.add(sprite);
arl.add(pepsi);
arl.add(orange);
arl.add(hershey);
arl.add(brownie);
arl.add(apple);
arl.add(peanut);
arl.add(trailmix);
arl.add(icecream);
arl.add(doughnut);
arl.add(banana);
arl.add(coffee);
arl.add(chips);
}
每当我编译时,我都会在第 141-153 行收到一条错误消息,指出
。 我知道这是一个基本问题,但我似乎无法弄清楚。非常感谢任何帮助。
I have a class named Storage. Storage contains an arraylist of special objects called Products. Each product contains information such as name, price, etc. My code is as follows:
class Storage{
Product sprite = new Product("sprite",1.25,30);
Product pepsi = new Product("pepsi",1.85,45);
Product orange = new Product("orange",2.25,36);
Product hershey = new Product("hershey",1.50,33);
Product brownie = new Product("brownie",2.30,41);
Product apple = new Product("apple",2.00,15);
Product crackers = new Product("peanut",3.90,68);
Product trailmix = new Product("trailmix",1.90,45);
Product icecream = new Product("icecream",1.65,28);
Product doughnut = new Product("doughnut",2.75,18);
Product banana = new Product("banana",1.25,32);
Product coffee = new Product("coffee",1.30,40);
Product chips = new Product("chips",1.70,35);
ArrayList<Product> arl = new ArrayList<Product>();
//add initial elements to arraylist
arl.add(sprite);
arl.add(pepsi);
arl.add(orange);
arl.add(hershey);
arl.add(brownie);
arl.add(apple);
arl.add(peanut);
arl.add(trailmix);
arl.add(icecream);
arl.add(doughnut);
arl.add(banana);
arl.add(coffee);
arl.add(chips);
}
Whenever I compile, I get an error message on lines 141-153 stating <identifier> expected
.
I know it's an elementary problem, but I can't seem to figure this out. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能仅在类主体中调用这样的方法。您必须将方法调用放入其他方法或构造函数中。
你想要这个:
You can't call methods like that just in the class body. You have to put methods calls in other methods, or in a constructor.
You want this:
问题是你的初始化代码不合适。您可以:
最后一个选项是最简单、最干净的解决方案;它看起来像这样:
另请注意,我将
arl
的类型切换为其接口List
。您应该尽可能尝试使用接口而不是具体的实现。The problem is that your initialization code is out of place. You can:
The last option is the simplest and cleanest solution; it would look something like this:
Note also that I switched the type of
arl
to its interfaceList<Product>
. You should try to work with interfaces rather than concrete implementations whenever possible.你的类缺少一些方法。使用构造函数或 main 方法 (public static void main(String[] args){...}) 来填充 ArrayList。
Your class is missing some methods. Use a constructor or a main method (public static void main(String[] args){...}) to fill your ArrayList.