java: <identifier> 预期与 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2576539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java: <identifier> expected with ArrayList
提问by A-moc
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:
我有一个名为 Storage 的类。存储包含称为产品的特殊对象的数组列表。每个产品都包含名称、价格等信息,我的代码如下:
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.
每当我编译时,我都会在第 141-153 行收到一条错误消息,指出<identifier> expected. 我知道这是一个基本问题,但我似乎无法弄清楚。任何帮助深表感谢。
回答by Cam
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:
你要这个:
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>();
//constructor
protected Storage(){
//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);
}
}
回答by polygenelubricants
The problem is that your initialization code is out of place. You can:
问题是你的初始化代码不合适。你可以:
- Put it in a constructor or other method
- Put it in instance initializer
- Put it as field initializer
- 将其放入构造函数或其他方法中
- 把它放在实例初始化程序中
- 把它作为字段初始值设定项
The last option is the simplest and cleanest solution; it would look something like this:
最后一个选项是最简单、最干净的解决方案;它看起来像这样:
List<Product> arl = new ArrayList<Product>(
Arrays.asList(
sprite, pepsi, orange, hershey, brownnie, apple, peanut,
trailmix, icecream, doughnut, banana, coffee, chips
)
);
Note also that I switched the type of arlto its interface List<Product>. You should try to work with interfaces rather than concrete implementations whenever possible.
另请注意,我将 的类型切换arl到其 interface List<Product>。您应该尽可能尝试使用接口而不是具体的实现。
回答by James P.
Your class is missing some methods. Use a constructor or a main method (public static void main(String[] args){...}) to fill your ArrayList.
你的班级缺少一些方法。使用构造函数或主方法 (public static void main(String[] args){...}) 来填充您的 ArrayList。

