【问题标题】:java: <identifier> expected with ArrayListjava: <identifier> 预期与 ArrayList
【发布时间】:2011-02-04 07:32:37
【问题描述】:

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

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 行收到一条错误消息,指出 &lt;identifier&gt; expected。 我知道这是一个基本问题,但我似乎无法弄清楚。非常感谢任何帮助。

【问题讨论】:

    标签: java object arraylist identifier


    【解决方案1】:

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

    你想要这个:

    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);
        }
    }
    

    【讨论】:

    • 当然。您可能也想尝试 polygenelubricants 的答案 - 它的重复性要低一些。
    【解决方案2】:

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

    • 放在构造函数或其他方法中
    • 将其放入实例初始化程序中
    • 把它作为字段初始化器

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

    List<Product> arl = new ArrayList<Product>(
      Arrays.asList(
        sprite, pepsi, orange, hershey, brownnie, apple, peanut,
        trailmix, icecream, doughnut, banana, coffee, chips
      )
    );
    

    还请注意,我将arl 的类型切换到其接口List&lt;Product&gt;。您应该尽可能使用接口而不是具体的实现。

    【讨论】:

    • 为什么要使用界面?这比直接使用 ArrayList 有什么好处?为 Arrays.asList 实例化 +1。
    • 除其他外,因为: (i) 更灵活;您不依赖于一种特定的实现(ii)更好的封装;您只能使用界面提供的内容。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 2017-11-03
      • 2019-02-06
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多