简体   繁体   English

返回arrayList的多个值

[英]Returning multiple values of arrayList

I have a method that needs to display history of bids entered into the system and what I have right now for the method is: 我有一种方法需要显示输入到系统中的出价的历史记录,而我现在要使用的方法是:

@Override
public String displayHistory() {
    String bidder = toString();
    if (this.bids.isEmpty()) {
        return bidder + " = no bids";
    }
    for (int i = 0; i < this.bids.size(); i++) {
        bidder = "\n" + this.bids.get(i).toString() + "\n";
    }
    return toString() + " = " + bidder;
}

toString method: (correct) toString方法:(正确)

@Override
public String toString() {
    return this.productId + ": " + this.productName;
}

What I need to the method to do is to print out in a form that passes my test class: 我需要的方法是通过通过我的测试课的表格打印出来:

Test class code: 测试类代码:

assertEquals("1: teddy = \n"+"J***e bid £7.0\n"+"S***a bid £5.0\n",product1.displayHistory());  

1: teddy = \\n J***e bid £7.00 \\n S***a bid $5.00 \\n 1:泰迪= \\ n J *** e竞标£7.00 \\ n S *** a竞标$ 5.00 \\ n

but currently my method is printing this: 但目前我的方法是打印此:

1: teddy = \\n J***e bid £7.00 \\n blank \\n 1:泰迪= \\ n J *** e出价£7.00 \\ n空白\\ n

however when I debug the program and check the array list at this point the second bid is inside but no displaying with the for loop. 但是,当我调试程序并检查数组列表时,第二个出价在内部,但没有显示for循环。

is there a way in which I can change this loop so that it print out all of the values of the array list in this and not just the last value that was entered into the list? 有没有一种方法可以更改此循环,以使其打印出此数组列表中的所有值,而不仅是最后输入到列表中的值? I assume it has to use a looping system but maybe not? 我认为它必须使用循环系统,但也许不是吗? Any help will be greatly appreciated. 任何帮助将不胜感激。

In your loop: 在您的循环中:

for (int i = 0; i < this.bids.size(); i++) {
    bidder = "\n" + this.bids.get(i).toString() + "\n";
}

you assign each time the current one bid to bidder variable. 您每次都将当前一个出价分配给bidder变量。 Instead, you should add like bidder += ... but concating a String is better yo do by StringBuilder.append() 相反,您应该添加如bidder bidder += ...但是通过StringBuilder.append()隐瞒字符串更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM