我的代码是这样的,但我在获取未售出的批处理时遇到了问题。我不想为 lotNumber 输入 int,因为我想显示所有未售出的商品。
我哪里出错了?
public ArrayList<Lot> getUnsold()
{
for(Lot lot : lots)
{
Bid highestBid = lot.getHighestBid();
lotNumber = lot.getNumber();
Unsold = new ArrayList<Lot>();
if (highestBid != null)
{
System.out.println("Lot number " + lotNumber + " is sold"); //retuern "Sold" is highestBid
}
else
{
System.out.println(lotNumber); //print bidder and highest bid value
}
}
return Unsold;
}
最佳答案
您需要在 for 循环之前实例化 Unsold
(应该是 unsold)。并且您没有向 else
block 中的 Unsold
列表添加任何内容。您需要将该批处理添加到 else
block 中的 Unsold
。
//assuming you are passing `lots` as parameter
public List<Lot> getUnsold(List<Lot> lots)
{
List<Lot> unsold = new ArrayList<Lot>();
for(Lot lot : lots)
{
Bid highestBid = lot.getHighestBid();
lotNumber = lot.getNumber();
if (highestBid != null)
{
System.out.println("Lot number " + lotNumber + " is sold"); //retuern "Sold" is highestBid
}
else
{
System.out.println(lotNumber); //print bidder and highest bid value
unsold.add(lot); // you are missing this
}
}
return unsold;
}
关于java - 试图获得未售出的批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234276/