[英]Remove Item from ArrayList
我正在尝试使用removeItem
方法从ArrayList
删除String的项目。 该项目被删除,但我仍然收到NullPointerException
,但不确定为什么。
/**
* remove item
*/
public void removeItem(String description)
{
Iterator<Item> i = items.iterator();
Item items = i.next();
if items.getDescription().equals(description) {
i.remove();
}
else {
System.out.println("Invalid item description");
}
System.out.println("Item removed!");
}
阅读本文如何比较Java中的字符串?
您应该使用.equals
来检查两个字符串是否相等。
并且,当您使用迭代器时,应使用迭代器的hasNext
方法检查是否有任何项目可用。
无论如何,还有其他方法,例如remove
!
您的代码将类似于:
public void removeItem(String description)
{
Iterator<Item> i = items.iterator();
while (i.hasNext())
{
Item items = i.next();
if (items.getDescription().equals(description))
{
i.remove();
System.out.print("Item removed!");
return;
}
}
}
return
因为如果已删除该项目,则应停止while
并退出该方法。
您应该检查items
和getDescription
是否不为null
!
有了您的代码,您将获得一个
“无效的项目描述”
无论如何。
试试看-使用Iterator的hasNext()
方法并使用null检查
Iterator<Item> i = items.iterator();
while (i.hasNext()) {
Item items = i.next();
if (items.getDescription() != null && items.getDescription().equals(description)) {
i.remove();
System.out.print("Item removed!");
return;
}
}
这有很多问题。 查看我添加到您的代码中的注释。
/**
* remove item
*/
public void removeItem(String description)
{
Iterator<Item> i = items.iterator(); //ok, so the variable items is probably a class field
Item items = i.next(); //...we just overwrote our class field items with the first item from iter...
if items.getDescription().equals(description) { //we don't have parenthesis around our if statement...?
i.remove(); //ok, we removed the item because it was equal
}
else {
System.out.println("Invalid item description"); //it wasn't equal, so we say we got an invalid description
}
//aaaand apparently we are done. We only checked the first item in "items"
System.out.println("Item removed!"); //this is outside of the if statement, so we will print this every time
}
因此,让我们清理这些问题。 似乎可能有多个具有相同描述的项目,所以让我们也更改方法以反映这一点。
/**
* removes all the items with a matching description
* returns the number of items removed
*/
public int removeItemsByDescription(String description) //make the method name reflect what it does
{
//do we want to be able to check against a null description? because this changes if that is the case
if(description == null)
throw new NullPointerException("description was null");
if(items == null) //make sure items is not null
throw new NullPointerException("Items was null");
Iterator<Item> itemsIter = items.iterator(); //more descriptive iterator name
Item item; //ok, we name our single item "item" - and we just declare it here, we'll assign it later
int removedItemNum = 0; //keep track of the total number of items we are removing
while(itemsIter.hasNext()){//let's go through all the items
item = = itemsIter.next(); //get the next item
if(description.equals(item.getDescription())){ //in order to avoid getting a null pointer exception when the description is null (which is probably perfectly valid), we will use description's .equals method
itemsIter.remove(); //remove the item
removedItemNum++; //increment the counter
}
//we don't need an else; if it didn't match, we just go on to the next one
}
System.out.println("Removed "+removedItemNum+" items");//print out how many we removed
}
然后我们去了! 完成测试后,您可能应该将最终的打印声明移出它。
就像我在评论中提到的那样,如果您希望能够删除带有空描述的项目(即,您希望将null
作为有效参数传递),则此方法会更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.