我正在尝试使用removeItem
方法从ArrayList
中删除字符串项。该项目已被删除,但我仍然得到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!");
}
答案 0 :(得分:0)
阅读此How do I compare strings in 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
!
使用您的代码,您将获得
无论如何,&#34;无效的商品说明&#34;
。
答案 1 :(得分:0)
尝试此操作 - 使用迭代器的hasNext()
方法&amp;使用空检查
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;
}
}
答案 2 :(得分:0)
这有很多问题。请参阅我在代码中添加的评论。
/**
* 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
}
然后我们去!完成测试后,您应该将最终的print语句移出它。
就像我在评论中提到的那样,如果您希望能够删除带有空描述的项目(即,您希望将null
作为有效参数传递),则此方法会更改。