我需要删除给定数字的地段。它似乎每次都返回null,或者没有找到很多回复。什么了?
问题可能出在其他地方而不是这段代码吗? removeLot是需要修复的方法。我在这里有点困惑。
public class Auction
{
// The list of Lots in this auction.
private ArrayList<Lot> lots;
// The number that will be given to the next lot entered
// into this auction.
private int nextLotNumber;
private ArrayList<Lot> Unsold;
private int lotNumber;
/**
* Create a new auction.
*/
public Auction()
{
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
/**
* Enter a new lot into the auction.
* @param description A description of the lot.
* Adds lot to ArrayList
*/
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
/**
* Show the full list of lots in this auction.
*/
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.toString());
}
}
/**
* Make a bid for a lot.
* A message is printed indicating whether the bid is
* successful or not.
*
* @param lotNumber The lot being bid for.
* @param bidder The person bidding for the lot.
* @param value The value of the bid.
* If successful bid it removes lot from ArrayList
*/
public void makeABid(int lotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) {
Bid bid = new Bid(bidder, value);
boolean successful = selectedLot.bidFor(bid);
if(successful) {
System.out.println("The bid for lot number " +
lotNumber + " was successful.");
}
else {
// Report which bid is higher.
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNumber +
" already has a bid of: " +
highestBid.getValue());
}
}
}
/**
* Return the lot with the given number. Return null
* if a lot with this number does not exist.
* @param lotNumber The number of the lot to return.
* No longer determines the lot number according to index number.
*/
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
// Don't return an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
/**
* Look for closed lots. Return highest bid and bidder name if sold.
* If lot not sold print not sold.
*/
public void close(int lotNumber, String description)
{
for(Lot lot : lots)
{
System.out.println(lotNumber + description); //print lot number and description.
Bid highestBid = lot.getHighestBid(); //get the highest bid for the lot.
if (highestBid != null)
{
String name = highestBid.getBidder().getName();
System.out.println(name + " " + highestBid.getValue()); //print bidder and highest bid value
}
else
{
System.out.println("Not sold"); //if not sold print "Not sold"
}
}
}
/**
* Returns the list of unsold lots.
* If sold print sold statement.
*/
public ArrayList<Lot> getUnsold()
{
ArrayList<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;
}
/**
* Remove the lot with the given lot number.
* @param number The number of the lot removed.
* @return The Lot with the given number, or null if there is no such lot.
*/
public Lot romoveLot(int number)
{
if((number >= 1) && (number < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(number);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != number) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
number);
// Don't return an invalid lot.
selectedLot = null;
}
else {
lots.remove(number);
}
return selectedLot;
}
else {
System.out.println("Lot number: " + number +
" does not exist.");
return null;
}
}
}
答案 0 :(得分:1)
我建议根据您的需要使用HashMap
或任何其他哈希表实现。
像HashMap<Lot> lots= new HashMap<Lot>();
将所有Lot
个变量存储到地图中,只需应用
lot.contains(numberYouWantToCheck);