Java 搜索arraylist,从arraylist中删除项目,然后将其添加到其他位置

Java 搜索arraylist,从arraylist中删除项目,然后将其添加到其他位置,java,arraylist,Java,Arraylist,public void drop(字符串名称)-如果合适,从ArrayList中删除该项并将其添加到当前文件室。使用以下选项之一更新游戏消息:1)玩家未持有该物品,2)房间已经有物品,或3)玩家已成功将物品放入房间。这是该方法的目标,但当我运行它时,它总是跳转到else语句中的currentMessage 问题: 我遇到的问题是,当我运行此方法并尝试在房间中删除一个项目时,它不会跳转到else语句并重新返回消息“you not have that Item”,我不知道为什么它会这样做,而不会执行

public void drop(字符串名称)-如果合适,从ArrayList中删除该项并将其添加到当前文件室。使用以下选项之一更新游戏消息:1)玩家未持有该物品,2)房间已经有物品,或3)玩家已成功将物品放入房间。这是该方法的目标,但当我运行它时,它总是跳转到else语句中的currentMessage

问题: 我遇到的问题是,当我运行此方法并尝试在房间中删除一个项目时,它不会跳转到else语句并重新返回消息“you not have that Item”,我不知道为什么它会这样做,而不会执行第一个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";
        }
    }
}

您的问题是,在遍历数组时,不允许您编辑该数组。像这样更改for循环以消除错误。此外,您正在使用if循环wronly。不要要求完整的条件为假,而只要求你想要的条件为假,并编写
在它之前

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";
        }
    }
}
public void drop(字符串名称)
{      
对于(int i=0;i
您的问题是,在迭代数组时,不允许您编辑该数组。像这样更改for循环以消除错误。此外,您正在使用if循环wronly。不要要求完整的条件为假,而只要求你想要的条件为假,并编写
在它之前

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";
        }
    }
}
public void drop(字符串名称)
{      
对于(int i=0;i
这将抛出一个
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();
    }
}
public void drop(字符串名称)
{   
迭代器it=myArray.Iterator();
项目计数=it.next();
while(count!=null){
if(count.getName().contains(name)&¤tRoom.hasItem()==false){
currentRoom.addItem(计数);
currentMessage=“您已成功将物品放入房间”;
it.remove();
}
else if(count.getName().contains(name)&¤tRoom.hasItem()=true)
{
currentMessage=“房间已经有一个项目”;
}
其他的
{
currentMessage=“您没有该项”;
}
count=it.next();
}
}

这将抛出一个
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();
    }
}
public void drop(字符串名称)
{   
迭代器it=myArray.Iterator();
项目计数=it.next();
while(count!=null){
if(count.getName().contains(name)&¤tRoom.hasItem()==false){
currentRoom.addItem(计数);
currentMessage=“您已成功将物品放入房间”;
it.remove();
}
else if(count.getName().contains(name)&¤tRoom.hasItem()=true)
{
currentMessage=“房间已经有一个项目”;
}
其他的
{
currentMessage=“您没有该项”;
}
count=it.next();
}
}
试试这个

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";       
}
public void drop(字符串名称)
{
for(Iterator it=col.Iterator();it.hasNext();)
{
项目计数=it.next();
if(count.getName().contains(name))
{
if(currentRoom.hasItem()==false)
{
currentRoom.addItem(计数);
currentMessage=“您已成功将物品放入房间”;
it.remove();
return;//一旦找到return;
}
其他的
{
currentMessage=“房间已经有一个项目”;
return;//找到后返回,或者继续查找
}
}           
}
//找不到物品
currentMessage=“您没有该项”;
}
除了ConcurrentModificationException之外,您的代码还有一个逻辑缺陷,它会在每次迭代后设置消息,而您可能希望它在设置currentMessage之前查看整个列表。

试试这个

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";       
}
public void drop(字符串名称)
{
for(Iterator it=col.Iterator();it.hasNext();)
{
项目计数=it.next();
if(count.getName().contains(name)