转换后的数组可以与另一个转换后的数组进行比较

时间:2013-03-10 22:24:07

标签: java arrays

好的,我试图正确地说出这个问题,以便得到我需要的帮助。我得到的是一个简单的钱包/硬币程序,它将String数组与另一个String数组进行比较。原谅漫长的循环/嵌套循环/逻辑,因为在这个赋值中我不能使用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

2 个答案:

答案 0 :(得分:1)

你的循环永远不会在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;
    }

如果先对List进行排序,然后使用第一种最佳的方法,那么这种方法效率显然非常低。但鉴于您无法使用Array方法,我认为您无法使用TreeSetCollections.sort()

答案 1 :(得分:1)

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

此代码始终返回false,因为它不会比较两个数组的内容,它会测试两个表达式是否引用相同的数组(即修改一个将修改另一个数组)。

基本上,您需要计算每个列表中每种硬币的数量,然后比较结果。如果事先知道可能种类的硬币,那很容易:每种硬币只有两个变量(一个用于这个钱包,一个用于另一个钱包)。如果不知道可能的硬币扭结,则必须使用Map