-1

So im working on some homework and I can't figure out how to make an String array[a] pass into a method in another class and be recorded as a String array there. This is kinda hard to explain since its new to me, but here is what I'm trying to do.

Tester Program:

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

Purse class:

public boolean sameCoins(Purse other)
    {
        if (array1.length != array2.length)
        {
            return false;
        }
        int same = 0;
        for (int i = 0; i < array1.length; i++)
        {
            for (int x = 0; x < array2.length; x++)
            {
                if (array2[x].equals(array1[i]))
                {
                    same++;
                    break;
                }
            }
        }
        return same==array1.length;
    }

I know array1 and array2 aren't proper variable/names for the arrays but that just to substitute the idea I'm trying to get at.

4

2 回答 2

1

如果arrayPurse类中的字段,other则可以在函数中访问钱包sameCoins数组other.array

于 2013-03-10T19:45:33.053 回答
0

您正在返回 same==array1.length 并且您最初设置了 same = 0 并且从未增加它。

于 2013-03-10T19:36:30.500 回答