英文:
Add recently deleted value in an arraylist back in java
问题
我创建了另一个名为 Item 的类,其中包含描述(字符串)和价格(整数)两个参数。在一个名为 Seller 的单独类中,使用 Item 来将一组物品保存到特定的卖家中。一切运行都没有错误,但返回的值让我感到困惑。
一个方法将物品添加到 ArrayList 中,另一个方法在物品被“出售”后将其移除。第三个方法将刚刚被移除的物品重新添加回 ArrayList 中。
我有两个问题,
1.如果在 sellItem 中应该返回什么,如果应该返回的只是被移除的物品(已售出的物品)。
2.如果参数只是上面返回的值(不包括描述或价格),我如何将物品添加回去。在测试代码中,给出了 i = s.sellItem("one-bedroom condo");
以及 s.acceptReturnedItem(i);
,所以参数是之前使用的变量。
以下是我的代码:
ArrayList<Item> items = new ArrayList<Item>();
public void addItem(String description, int price) {
Item i = new Item(description, price);
items.add(i);
return;
}
public Item sellItem(String description) {
for (Item v : items) {
if (v.getDescription() == description) {
items.remove(v);
return v; // 返回被移除的物品
}
}
return null; // 如果找不到对应物品,可以返回 null 或者抛出异常
}
public void acceptReturnedItem(Item x) { // 参数 x 是上面返回的物品
items.add(x); // 直接将物品添加回 ArrayList 中
return;
}
请注意,我已经进行了一些修改,以便在合适的地方返回了相应的物品或者 null 值(如果未找到)。另外,在接收返回物品的方法中,我移除了多余的返回语句,因为您只需要将物品添加回 ArrayList 中即可。
英文:
I have created another class called Item that has the parameters description (string) and price (int). In a separate class, called Seller, Item is used to save a list of items to the specific seller. Everything runs without error, but the values returned is what is throwing me off.
<br />
One method adds the items to the ArrayList, and another removes an item once it is "sold". A third method adds the item that was just removed back to the arraylist.
<br />
I have two questions, <br />
1.What should be returned in sellItem if what should be there is just the item that was removed (sold). <br />
2.How would I add the item back in when the only parameter is the returned value from above (not description or price). In the tested code, i = s.sellItem("one-bedroom condo");
is given along with s.acceptReturnedItem(i);
so that the parameter is the variable used previously. <br />
Here is my code:
ArrayList <Item> items = new ArrayList <Item>();
public void addItem(String description, int price) {
Item i = new Item(description, price);
items.add(i);
return;
}
//adds item with two parameters
public Item sellItem(String description) {
for (Item v: items) {
if (v.getDescription() == description) {
items.remove(v);
}
}
return new Item(?); //what are we returning???
}
public Item acceptReturnedItem(Item x) { //where x is the returned item from above
//Item f = new Item(addItem); not sure if this is correct
//items.add(f); would add it??
return new Item(?);
}
答案1
得分: 0
- 您必须创建一个Item对象,并将其分配给在ArrayList中找到的Item,然后从列表中移除它。
public Item sellItem(String description) {
for (Item v : items) {
if (v.getDescription() == description) {
items.remove(v);
return v;
}
}
return null;
}
- 您不需要从
acceptReturnedItem(Item f)
方法中返回任何内容。
public void acceptReturnedItem(Item f) {
items.add(f);
}
如果您需要返回Item,则可以像以下方式编写该方法:
public Item acceptReturnedItem(Item f) {
items.add(f);
return f;
}
英文:
Below are the changes you must incorporate
-
You must create an Item object and assign to it the Item identified in the ArrayList and then remove it from the list
public Item sellItem(String description) {
for (Item v: items) { if (v.getDescription() == description) { items.remove(v); return v; } } return null;
}
-
You need not return anything from the
acceptReturnedItem(Item f)
methodpublic void acceptReturnedItem(Item f) {
items.add(f);
}
If you need to return the Item then you can code the method like below
public Item acceptReturnedItem(Item f) {
items.add(f);
return f;
}
答案2
得分: 0
-
当您找到物品时,应该中断循环,以便可以返回相同的物品。您必须将已移除的物品作为函数的返回类型返回。
-
我们正在返回一个物品,而不仅仅是一个变量。变量 i 的类型是 item。
所以您必须将物品作为参数,然后将其添加回列表。(在下面完成)
public Item sellItem(String description) {
Item found = null;
for (Item v : items) {
if (v.getDescription() == description) {
found = v;
items.remove(v);
break; //当找到物品时,应中断循环,以便可以返回相同的物品
}
}
return found; //我们在返回什么?- 现在您正在返回已移除的物品。
}
public void acceptReturnedItem(Item x) {
items.add(x); //将从 sellItem 得到的物品添加回来,我认为您这里不需要返回任何东西
}
英文:
1)You should break the loop when u found the item so that you can return the same item. U have to return the removed item as the return type of the function is item.
-
We are returning an item, not just a variable. The i variable is of type item.
so you have to take the item as parameter and add it back to the list.(done below)public Item sellItem(String description) {
Item found = null;
for (Item v: items) {
if (v.getDescription() == description) {
found = v;
items.remove(v);
break; //you should break the loop when u found the item so that you can return the same item
}
}
return found; //what are we returning??? - now u r returning the removed item.
}public void acceptReturnedItem(Item x) {
items.add(x); //adding back the item which we got from sellitem, i think u dont need to return anything here
}
答案3
得分: 0
方法 sellItem()
应该如下所示:
public Item sellItem(String description)
{
Item retValue = null;
for (Item v : items)
{
if (v.getDescription().equals(description))
{
retValue = v;
// 在这里终止循环…
break;
}
}
if (retValue != null) items.remove(retValue);
return retValue;
}
在 foreach 循环中调用 items.remove()
是有条件地不安全的;如果循环之后会继续执行,这会引发 ConcurrentModificationException
异常。
或者,你可以像这样实现它:
public Item sellItem(String description)
{
Item retValue = null;
for (Iterator<Item> i = items.iterator(); i.hasNext() && (retValue == null); )
{
Item v = i.next();
if (v.getDescription().equals(description))
{
retValue = v;
i.remove();
// 这里不需要断开循环…
}
}
return retValue;
}
在循环内部调用 Iterator.remove()
是安全的。
在内部,foreach 循环的实现类似于第二个建议;请查看接口 Iterable
。
英文:
The method sellItem()
should look like this:
public Item sellItem( String description )
{
Item retValue = null;
for( Item v : items )
{
if( v.getDescription().equals( description ) )
{
retValue = v;
// you need to break the loop here …
break;
}
}
if( retValue != null ) items.remove( retValue );
return retValue;
}
Calling items.remove()
in the foreach loop is conditionally unsafe; it causes a ConcurrentModificationException
if the loop will continue afterwards.
Alternatively, you can implement it like this:
public Item sellItem( String description )
{
Item retValue = null;
for( Iterator<Item> i = items.iterator(); i.hasNext() && (retValue == null); )
{
Item v = i.next();
if( v.getDescription().equals( description ) )
{
retValue = v;
i.remove();
// No break necessary here …
}
}
return retValue;
}
Calling Iterator.remove()
inside the loop is safe.
Internally, the foreach loop looks like the second suggestion; have a look to the interface Iterable
.
专注分享java语言的经验与见解,让所有开发者获益!
评论