4

Storageという名前のクラスがあります。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 行目に<identifier> expected. 初歩的な問題であることはわかっていますが、これを理解できないようです。どんな助けでも大歓迎です。

4

3 に答える 3

8

クラス本体だけでそのようなメソッドを呼び出すことはできません。メソッド呼び出しを他のメソッドまたはコンストラクターに配置する必要があります。

あなたはこれを求めている:

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);
    }
}
于 2010-04-05T01:37:03.677 に答える
5

問題は、初期化コードが適切でないことです。あなたはできる:

  • コンストラクターまたは他のメソッドに入れます
  • インスタンス初期化子に入れる
  • フィールド初期化子として配置

最後のオプションは、最も単純でクリーンなソリューションです。次のようになります。

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

arlのタイプをそのインターフェースに切り替えたことにも注意してくださいList<Product>。可能な限り、具体的な実装ではなくインターフェイスを使用するようにしてください。

于 2010-04-05T01:46:43.310 に答える
1

クラスにいくつかのメソッドがありません。コンストラクターまたはメイン メソッド (public static void main(String[] args){...}) を使用して、ArrayList を埋めます。

于 2010-04-05T02:45:07.977 に答える