数组,toString?

时间:2013-09-23 15:45:57

标签: java arrays return tostring

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

    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 [] coins)的方法中,但是在同一个类中。

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

4 个答案:

答案 0 :(得分:4)

以完整示例编辑

你的硬币课应该是这样的:

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枚举可能看起来像这样:

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

答案 1 :(得分: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(); 

}

答案 2 :(得分: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]... ]

答案 3 :(得分:-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