2013-03-05 30 views
1

我的代码是这样的,但我有一个问题得到未售出的很多。我不想输入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; 
} 
+0

哪里是'lots'初始化?它应该作为参数传入吗? – ninesided 2013-03-05 21:24:46

+0

很多在ArrayList 。 – machiavelli 2013-03-05 21:33:50

+0

我知道类型,这是从for-each循环中显而易见的,我只是指出它没有被初始化或传入方法。请参阅@ Rp-的答案,他们假设你打算通过它! – ninesided 2013-03-05 21:48:42

回答

6

您需要在for循环前实例化Unsold(应该未售出)。并且您没有在else区块的Unsold列表中添加任何内容。您需要将该批次添加到Unsoldelse区块中。

//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; 
    }