首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java: ArrayList需要<identifier>

java: ArrayList需要<identifier>
EN

Stack Overflow用户
提问于 2010-04-05 09:32:51
回答 3查看 22.3K关注 0票数 4

我有一个名为Storage的类。存储包含称为产品的特殊对象的数组列表。每个产品都包含名称、价格等信息,我的代码如下:

代码语言:javascript
复制
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行收到一条错误消息,指出<identifier> expected。我知道这是一个基本的问题,但我似乎想不出来。任何帮助都是非常感谢的。

EN

回答 3

Stack Overflow用户

发布于 2010-04-05 09:37:04

你不能仅仅在类体中调用这样的方法。您必须将方法调用放在其他方法中,或者放在构造函数中。

你想要这个:

代码语言:javascript
复制
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);
    }
}
票数 8
EN

Stack Overflow用户

发布于 2010-04-05 09:46:43

问题是你的初始化代码不合适。您可以:

  • 将其放入构造函数或其他方法中,
  • 将其放入实例中,并将其作为字段初始值设定项

最后一个选项是最简单、最干净的解决方案;它看起来像这样:

代码语言:javascript
复制
List<Product> arl = new ArrayList<Product>(
  Arrays.asList(
    sprite, pepsi, orange, hershey, brownnie, apple, peanut,
    trailmix, icecream, doughnut, banana, coffee, chips
  )
);

另请注意,我将arl的类型切换为其接口List<Product>。只要有可能,您应该尝试使用接口而不是具体的实现。

票数 5
EN

Stack Overflow用户

发布于 2010-04-05 10:45:08

您的类缺少一些方法。使用构造函数或main方法(public static void main(String[] args){...})填充您的ArrayList。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2576539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档