簡體   English   中英

Java的: <identifier> 期待與ArrayList

[英]java: <identifier> expected with ArrayList

我有一個名為Storage的類。 存儲包含一個名為Products的特殊對象的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行收到一條錯誤消息,說明<identifier> expected 我知道這是一個基本問題,但我似乎無法解決這個問題。 任何幫助深表感謝。

你不能只在類體中調用這樣的方法。 您必須將方法調用放在其他方法或構造函數中。

你要這個:

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

問題是您的初始化代碼不合適。 您可以:

  • 把它放在構造函數或其他方法中
  • 把它放在實例初始化器中
  • 把它作為字段初始化程序

最后一個選擇是最簡單,最干凈的解決方案; 它看起來像這樣:

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

另請注意,我將arl的類型切換到其接口List<Product> 您應盡可能嘗試使用接口而不是具體實現。

你的班級缺少一些方法。 使用構造函數或main方法(public static void main(String [] args){...})來填充ArrayList。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM