0

こんにちは、どこが間違っているのか誰か教えてもらえますか?

このクラスの基本的な目的は、この場合は車に関するお気に入りアイテムの配列リストを定義することです。車のオブジェクトには、車の名前と車 1 ~ 5 の評価があります。

文字列が車のオブジェクトの評価と等しいかどうかを確認するにはどうすればよいですか? 文字列または整数を配列リストの車のオブジェクトと比較する部分を台無しにしています。equals() メソッドの何が問題になっていますか? contains() メソッドは同じように機能しますか?

numberOfItemsOfRating メソッドを使用すると、ユーザーは評価を指定できるため、このメソッドは評価のある車を返しません。searchForItems メソッドは、指定された文字列の説明が配列リスト内の車の名前と一致するかどうかをチェックするため、配列リスト内の車を返します。

コンストラクターと変数を使用した 2 つのメソッドの概要を次に示します。

public class FavouriteItems
{
    private ArrayList<Item> cars; 

    /**
     * Constructor for objects of class FavouriteItems
     */
    public FavouriteItems()
    {
        cars= new ArrayList<Item>();

    }

    /**
     * Add a new Item to your collection
     * @param newItem The Item object to be added to the collection.
     */
    public void addToFavourites(Item newItem) 
    {
        cars.add(newItem);

    }
    /**
     * Count the number of Items with a given rating 
     * @return The number of Items (Item objects) 
     *          whose rating is rating (could be 0).
     *          If the rating parameter is outside the valid
     *          range 1..5 then print an error message and return 0.
     */
    public int numberOfItemsOfRating(int rating)
    {
        int counter = 0;
        if(rating >= 1 && rating <=5) 
        {
            for ( int i =0; i < cars.size(); i++)
            {
                int num = rating;
                String al = Integer.toString(rating);
                if(cars.get(i).equals(al))
                {
                    counter++;
                }
            }
        }
        else 
        {
            System.out.println("No cars match your ratings");
            counter = 0;
        }
        return counter;
    }

    /**
     * Find the details of a Item given its description
     * @return Item object if its description is in the collection
     * or null if there is no item with that description
     */
    public Item searchForItem(String description) 
    {
         for(int i=0; i<cars.size(); i++)
        { 
            if(cars.equals(description))
            { 
                 return cars.get(i);
            } 
            else 
            { 
                return null;
            }
        }  
      }
} 
4

5 に答える 5

1

オブジェクト自体に基づいて同等性チェックを実行していますが、代わりにオブジェクトのプロパティに対して実行する必要があります。特定のケースでratingは、コレクション内の各車/アイテムの属性を確認する必要があります。コードは次のようになります。

final String ratingStr = Integer.toString(rating);

int counter = 0;
for (for final Item car: cars) {
    if(ratingStr.equals(car.getRating()) {
        ++counter;
}

System.out.println("Number of 'cars' with the rating is: " + counter);

2つの簡単なコメントですが、クラスに等式メソッドを実装する必要Itemがあります。しかし、この場合、それはあなたの問題の実際の原因ではありません。また、コードで車について多く言及していますが、Beanクラスは「Item」と呼ばれています。コードを読む他の人を混乱させる可能性があるため、これを調整することをお勧めします。

メソッドも修正することを忘れないでくださいsearchForItem。現在、配列リストと文字列の同等性をテストしていますが、これはtrueを返すことはありません。上記と同じ方法で修正しdescriptionますが、属性の代わりに車の属性を使用しratingます。

于 2012-04-09T15:30:09.957 に答える
0
cars.get(i)

文字列ではなく、アイテムを返します。それで

if(cars.get(i).equals(al))

正しくありません。

于 2012-04-09T15:24:34.647 に答える
0
if(cars.equals(description))

ArrayList cars(この場合はリスト全体)が単一の文字列と等しくなることはありません。

車を検索する場合は、リスト内のすべてのアイテムをチェックして、それらの名前(またはクラスに保存する情報Item)が指定されたに一致するかどうかを確認する必要がありますdescription

于 2012-04-09T15:26:16.053 に答える
0

これは、equalsメソッドを使用する方法ではありません。代わりに、Item#getRating()andを使用または実装することをお勧めしますItem#getDescription()cars.get(i).getDescription().equals(description)説明を確認するために使用します。評価を確認するには、を使用しますcars.get(i).getRating() == rating

アイテムを文字列と比較するためにequalsを使用しないでください。これは、equalsコントラクトに違反するためです。

于 2012-04-09T15:26:29.503 に答える