搜索arraylist,从arraylist中删除项目,将其添加到其他地方

时间:2014-11-28 17:53:18

标签: java arraylist

public void drop(String name) - 如果合适,从ArrayList中删除该项并将其添加到当前房间。使用以下选项之一更新游戏的消息:1)玩家没有持有该物品,2)房间已经有物品,或3)玩家已成功将物品丢弃在房间内。这是此方法的目标,但是当我运行它时,它总是跳到else语句中的currentMessage。

问题: 我正在讨论的问题是,当我运行此方法并尝试在一个房间中删除一个项目时,它不会跳到else语句并重新发出消息“你没有那个项目”,我不知道为什么它这样做并没有通过第一个if语句,因为我输入的项目名称我知道是在arraylist中。

public void drop(String name)
{      
    for(Item count : myArray){
        if(count.getName().contains(name) && currentRoom.hasItem() == false){
            currentRoom.addItem(count);
            currentMessage = "you have successfully dropped the item in the room";
            myArray.remove(count);
        }
        else if(count.getName().contains(name) && currentRoom.hasItem() == true)
        {
            currentMessage = "the room already has an item";
        }
        else 
        {
            currentMessage = "you do not have that item";
        }
    }
}

3 个答案:

答案 0 :(得分:3)

这将抛出ConcurrentModificationException因为您在修改列表时无法使用foreach循环。相反,迭代器支持Iterator.remove()方法,该方法允许您从基础集合中删除对象:

public void drop(String name)
{   
    Iterator<Item> it = myArray.iterator();
    Item count = it.next();
    while(count != null){
        if(count.getName().contains(name) && currentRoom.hasItem() == false){
            currentRoom.addItem(count);
            currentMessage = "you have successfully dropped the item in the room";
            it.remove();
        }
        else if(count.getName().contains(name) && currentRoom.hasItem() == true)
        {
            currentMessage = "the room already has an item";
        }
        else 
        {
            currentMessage = "you do not have that item";
        }
        count = it.next();
    }
}

答案 1 :(得分:1)

您的问题是您在迭代时不允许编辑阵列。像这样更改你的for循环以消除错误。你也正在使用if循环。不要要求完整的条件是假的,而只要在你之前写一个!就是假的。

public void drop(String name)
{      
    for (int i = 0; i < myArray.size(); i++) {
        Item count = myArray.get(i);
        if (count.getName().contains(name) && !currentRoom.hasItem()){
            currentRoom.addItem(count);
            currentMessage = "you have successfully dropped the item in the room";
            myArray.remove(count);
            i--; // element removed, so decrease count
        }
        else if(count.getName().contains(name) && currentRoom.hasItem() == true)
        {
            currentMessage = "the room already has an item";
        }
        else 
        {
            currentMessage = "you do not have that item";
        }
    }
}

答案 2 :(得分:0)

试试这个;

public void drop(String name)
{
    for (Iterator<Item> it = col.iterator(); it.hasNext();)
    {
        Item count = it.next();
        if(count.getName().contains(name))
        {
            if(currentRoom.hasItem() == false)
            {
               currentRoom.addItem(count);
               currentMessage = "you have successfully dropped the item in the room";
               it.remove();
               return; //Once found return;
            }
            else
            {
               currentMessage = "the room already has an item";
               return; //Once found return or alternatively keep looking
            }
        }           
    }
    //Item never found
    currentMessage = "you do not have that item";       
}

除了ConcurrentModificationException之外,你的代码还有一个逻辑缺陷,它在每次迭代后设置消息,而你可能希望它在设置currentMessage之前查看整个列表。