0

わかりました、必要な助けが得られるように、質問を正しく表現しようとしていました。私が得たのは、文字列配列を別の文字列配列と比較する単純な財布/コイン プログラムです。この割り当てでは、Java 配列およびコレクション クラスのメソッドを使用できないため、長く引き出されたループ/ネストされたループ/ロジックを許してください。これはクラスの課題なので、答えるだけでなく、プロセスを説明してください。

(理論): 2 つの変換された配列の比較が問題の原因であると考えていましたが、配列リストの各要素を他の配列リストの各要素と比較する方法がわかりませんでした。

Purse.class:

import java.util.ArrayList;

/**
 * A purse holds a collection of coins.
 */
public class Purse
{
    private ArrayList<String> coins;

    /**
     * Constructs an empty purse.
     */
    public Purse()
    {
        coins = new ArrayList<String>();
    }

    /**
     * Add a coin to the purse.
     * 
     * @param coinName
     *            the coin to add
     */
    public void addCoin(String coinName)
    {
        coins.add(coinName);
    }

    /**
     * Returns a string describing the object.
     * 
     * @return a string in the format "Purse[coinName1,coinName2,...]"
     */
    public String toString()
    {
        String coinName1 = "Quarter";
        String coinName2 = "Dime";
        String coinName3 = "Nickel";
        String coinName4 = "Penny";

        String str = "Actual:"
                + "Purse["
                + (coinName1 + "," + coinName2 + "," + coinName3 + "," + coinName2)
                + "]";

        return str;
    }

    /**
     * Determines if a purse has the same coins in the same or different order
     * as another purse.
     * 
     * @param other
     *            the other purse
     * @return true if the two purses have the same coins in the same or
     *         different order, false otherwise
     */
    public boolean sameCoins(Purse other)
    {
        if (this.coins.size() != other.coins.size())
        {
            System.out.println("1");
            return false;
        }


        int matched = 0;
        for (int i = 0; i < this.coins.size(); i++)
        {
            for (int j = 0; j < other.coins.size(); j++)
            {
                if (this.coins.toArray() == other.coins.toArray())
                {
                    matched++;
                    System.out.println("2");
                    System.out.println(this.coins.toArray());
                    System.out.println(other.coins.toArray());
                    break;
                }
            }
        }
        return matched == this.coins.size();

    }

} 

PurseTester.class:

/**
 * This class tests the Purse class.
 */
public class PurseTester
{
    public static void main(String[] args)
    {
        Purse p = new Purse();
        p.addCoin("Quarter");
        p.addCoin("Dime");
        p.addCoin("Nickel");
        p.addCoin("Dime");

        System.out.println(p.toString());
        System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");

        Purse a = new Purse();
        a.addCoin("Quarter");
        a.addCoin("Dime");
        a.addCoin("Nickel");
        a.addCoin("Dime");

        Purse b = new Purse();
        b.addCoin("Nickel");
        b.addCoin("Dime");
        b.addCoin("Dime");
        b.addCoin("Quarter");

        System.out.println(a.sameCoins(b));
        System.out.println("Expected: true");

        Purse c = new Purse();
        c.addCoin("Quarter");
        c.addCoin("Penny");
        c.addCoin("Nickel");
        c.addCoin("Dime");

        Purse d = new Purse();
        d.addCoin("Nickel");
        d.addCoin("Dime");
        d.addCoin("Dime");
        d.addCoin("Quarter");

        System.out.println(c.sameCoins(d));
        System.out.println("Expected: false");

    }
}

出力は次のとおりです。

Actual:Purse[Quarter,Dime,Nickel,Dime]
Expected: Purse[Quarter,Dime,Nickel,Dime]
false
Expected: true
false
Expected: false

期待される出力:

Actual:Purse[Quarter,Dime,Nickel,Dime]
Expected: Purse[Quarter,Dime,Nickel,Dime]
true
Expected: true
false
Expected: false
4

2 に答える 2

1
this.coins.toArray() == other.coins.toArray()

このコードfalseは、2 つの配列の内容を比較せず、2 つの式が同じ配列を参照しているかどうかをテストする (つまり、一方を変更すると他方も変更される) かどうかをテストするため、常に返されます。

基本的には、各リスト内の各種類のコインの数を数えて、結果を比較する必要があります。考えられるコインの種類が事前にわかっている場合は、簡単です。コインの種類ごとに 2 つの変数を用意するだけです (この財布用に 1 つ、もう 1 つの財布用に 1 つ)。コインのねじれの可能性がわからない場合は、Map.

于 2013-03-10T22:38:59.923 に答える
1

あなたのループは s を決して見ません.2つの配列Listを繰り返しチェックするだけです. ==次のような要素を比較する必要があります。

    for (int i = 0; i < this.coins.size(); i++)
    {
        if (!this.coins.get(i).equals(other.coins.get(i)))
        {
           return false;
        }
    }

ただし、これは、同じ順序が必要であると仮定して比較します。順序を無視して比較する必要がある場合は、他の配列をループして、見つかった場合は要素を削除する必要がありますreturn false

    final List<String> copy = new ArrayList<String>(other.coins);
    outerLoop:
    for (final String mine : coins)
    {
        final Iterator<String> otherIter = copy.iterator();
        while (otherIter.hasNext())
        {
            if (mine.equals(otherIter.next()))
            {
                otherIter.remove();
                continue outerLoop;
            }
        }
        return false;
    }

この方法は、最初に s をソートしてから、List最適な最初の方法を使用した場合、明らかに非常に非効率的です。しかし、Arrayメソッドを使用できないことを考えると、TreeSetまたはを使用できないと思いますCollections.sort()

于 2013-03-10T22:36:48.553 に答える