0

所以我想做的是打印出这个数组。

    coins[0] = new Coin(CoinName.PENNY, 2011);
    coins[1] = new Coin(CoinName.DIME, 1952);
    coins[2] = new Coin(CoinName.DOLLAR, 1999);
    coins[3] = new Coin(CoinName.NICKEL, 2004);
    coins[4] = new Coin(CoinName.DIME, 1952);
    coins[5] = new Coin(CoinName.DOLLAR, 1945);
    coins[6] = new Coin(CoinName.PENNY, 2011);
    coins[7] = new Coin(CoinName.QUARTER, 1961);
    coins[8] = new Coin(CoinName.NICKEL, 1983);
    coins[9] = new Coin(CoinName.FIFTY_CENT, 1963);

但我知道我可以使用 Arrays.toString 方法,但这次我不能使用它。我必须使用我自己的名为

public String toString()

这就是它需要完全设置的方​​式,如何做的说明在评论中。

//the standard toString method that returns a String listing all the coins in the jar,   in their current order
public String toString()
{
    return ???;

}

数组的初始化在一个名为 CoinJar(Coin[] coin) 的方法中,但在同一个类中。

我不确定我问这个问题是否正确,如果你们需要任何细节,请询问。

4

4 回答 4

4

用一个完整的例子编辑

您的 Coin 类应如下所示:

public class Coin {
    private CoinName coinName;
    private int year;

    public Coin(CoinName coinName, int year) {
        this.coinName = coinName;
        this.year = year;
    }

    @Override
    public String toString() {  
        return coinName + " " + year;
    }
}

你不知道该放什么的方法在那里,我做了这个类来测试我发布的答案:

public class CoinContainer {
    private Coin[] coins;

    public CoinContainer() {  
        coins = new Coin[10];

        coins[0] = new Coin(CoinName.PENNY, 2011);
        coins[1] = new Coin(CoinName.DIME, 1952);
        coins[2] = new Coin(CoinName.DOLLAR, 1999);
        coins[3] = new Coin(CoinName.NICKEL, 2004);
        coins[4] = new Coin(CoinName.DIME, 1952);
        coins[5] = new Coin(CoinName.DOLLAR, 1945);
        coins[6] = new Coin(CoinName.PENNY, 2011);
        coins[7] = new Coin(CoinName.QUARTER, 1961);
        coins[8] = new Coin(CoinName.NICKEL, 1983);
        coins[9] = new Coin(CoinName.FIFTY_CENT, 1963);
    }

    @Override
    public String toString() {
        String myString = "";

        for(Coin coin : coins) {
            myString += coin + "\n";
        }

        return myString;
    }

    public static void main(String[] args) {
        CoinContainer coinContainer = new CoinContainer();

        System.out.println(coinContainer);
    }
}

最后,为了获得完整的答案,您的 CoinName Enum 可能看起来像这样:

public enum CoinName {
    PENNY,
    DIME,
    DOLLAR,
    NICKEL,
    QUARTER,
    FIFTY_CENT
}

我测试了它,一切正常。这是我得到的输出:

PENNY 2011
DIME 1952
DOLLAR 1999
NICKEL 2004
DIME 1952
DOLLAR 1945
PENNY 2011
QUARTER 1961
NICKEL 1983
FIFTY_CENT 1963
于 2013-09-23T15:56:03.960 回答
0

在您的硬币类中,将 toString() 覆盖为以下内容:

public String toString()
{
    return this.coinName+ " " + this.year;
}

并打印数组内容你可以做这样的事情

public String toString()
{
    StringBuffer st = new StringBuffer();
    // or you could use this too...    StringBuilder st = new StringBuilder(); in a performance intensive system, where there is no need of synchronization this would be a little better.
    for(int index = 0; index < coins.length; index++ )
    {
        st.append("Coin "+index+" : "+coins[index].toString());
    }  
    return st.toString(); 

}
于 2013-09-23T15:53:52.573 回答
0

我猜 CoinName 是一个枚举,所以你的 Coin 对象应该覆盖 toString 方法

为确保正确覆盖它(如果不这样做,代码将无法编译),您必须使用 @Override 注释方法。

方法可能如下所示:

    @Override
    public String toString() {
        return new StringBuilder("[Name: ")
                .append(name.getName())
                .append(", Year: ")
                .append(year)
                .append("]")
                .toString();
    }

这将打印出漂亮的输出:

[[Name: PENNY, Year: 2011], [Name: DIME, Year: 1952]... ]
于 2013-09-23T16:15:42.630 回答
-1

重载类toString中的默认方法Coin。然后在您自己的toString方法中遍历coins数组并简单地

public String toString {
    private String s = "";
    for(int i =0; i < coins.length; i++)
        s += coins(i) + ", ";

    return s
}

如建议的那样,如果需要性能,请StringBuilder改用!

于 2013-09-23T15:52:51.520 回答