简体   繁体   English

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

[英]Search through arraylist, remove item from arraylist, add it elsewhere

public void drop (String name) - if appropriate, remove the item from the ArrayList and add it to the current room. public void drop(字符串名称)-如果合适,请从ArrayList中删除该项目并将其添加到当前房间。 Update the game's message with one of the following options: 1) the player is not holding that item, 2) the room already has an item, or 3) the player has successfully dropped the item in the room. 使用以下选项之一更新游戏的消息:1)玩家未持有该物品,2)房间中已有物品,或3)玩家已成功将物品放置在房间中。 This is the goal of this method but when I run it it always skipps to the currentMessage in the else statement. 这是该方法的目标,但是当我运行它时,它总是跳到else语句中的currentMessage上。

PROBLEM: The problem I am hacing is that when I run this method and try to drop an Item in a room, it doesnt and skips to the else statement and resturns the message "you do not have that item", and I do not know why it is doing this and not working through the first if statement because I am typing an items name I know is in the arraylist. 问题:我遇到的问题是,当我运行此方法并尝试将某个项目放在房间中时,它没有,并跳到else语句并返回消息“您没有该项目”,并且我不知道为什么这样做而不通过第一个if语句进行处理,因为我键入的是数组列表中的项目名称。

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";
        }
    }
}

This is going to throw a ConcurrentModificationException because you cannot use a foreach loop while modifying the list. 这将引发ConcurrentModificationException因为在修改列表时不能使用foreach循环。 Instead iterators support the Iterator.remove() method that will allow you to remove an object from the underlying collection: 相反,迭代器支持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();
    }
}

Your problem is that you are not allowed to edit the array while you are iterating through it. 您的问题是,遍历数组时不允许编辑数组。 Change your for loop like this to get rid of the error. 像这样更改您的for循环以消除该错误。 Also you are using the if loop wronly. 另外,您正在使用if循环wronly。 Don't ask the complete condition to be false but only the one you want to be false with writing an ! 不要要求完整的条件为假,而只写一个您想为假的条件即可! before it. 在它之前。

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";
        }
    }
}

Try this; 尝试这个;

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";       
}

In addition to the ConcurrentModificationException your code also had a logical flaw in it, it set the message after every iteration, whereas you probably wanted it to look through the entire list before setting currentMessage. 除了ConcurrentModificationException之外,您的代码还存在逻辑缺陷,它在每次迭代后设置消息,而您可能希望它在设置currentMessage之前仔细查看整个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM