[英]Removing a number from a listed created by given numbers
so here's the details. 这是详细信息。 I'm coding using java - BlueJ, and currently have a main class Auction, and a few other classes: Lot, Person, Bid
我正在使用java-BlueJ进行编码,并且目前有一个主要的拍卖类,以及其他几个类:Lot,Person,Bid
The purpose of the program is to enter items into the auction, along with their prices, where the information will be stored according to whether it's price, person, or item name/description. 该程序的目的是将物品及其价格输入拍卖中,然后根据价格,人或物品名称/描述将信息存储在拍卖中。
This is the code for my getLot method - user inputs lot number, and it shows info for the lot, which I assume should also be the starting point for the removeLot method since it should still check if the lot number given is valid first. 这是我的getLot方法的代码-用户输入批号,它显示批号的信息,我认为这也应该是removeLot方法的起点,因为它仍应首先检查给出的批号是否有效。
I'm trying to figure out how to add a removeLot method so that I can remove an item from the lot by typing in its lot number. 我试图弄清楚如何添加removeLot方法,以便可以通过输入批号从批号中删除项目。
This is the code I have for that section. 这是该部分的代码。
public Lot removeLot(int number)
{
if((number >= 1) && (number < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(number - 1);
// 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 {
Lot.removeIf(selectedLot.getNumber() = number);**
}
return selectedLot;
The else block with the "**" is what I added, wanting to remove the given number. 我添加了带有“ **”的else块,想要删除给定的数字。 But it's clearly wrong, and I'm not sure what to do.
但这显然是错误的,我不确定该怎么做。
If the datatype of lots
variable is List
than you can try below code 如果
lots
变量的数据类型为List
,则可以尝试以下代码
public Lot removeLot(int number)
{
Lot selectedLot=null;
if((number >= 1) && (number < nextLotNumber)) {
// The number seems to be reasonable.
selectedLot = lots.get(number - 1);
// 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 {
if(selectedLot.getNumber() = number)
{
lots.remove(number-1);
}
}
return selectedLot;
}
Lot.removeIf(selectedLot.getNumber() = number) is not correct Lot.removeIf(selectedLot.getNumber()=数字)不正确
you can try: 你可以试试:
} else if(selectedLot.getNumber() == number) {
Lot.removeIf(selectedLot.getNumber());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.