当前位置: 首页 > 知识库问答 >
问题:

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

李博达
2023-03-14

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

问题:我遇到的问题是,当我运行这个方法并试图将一个项目放到房间中时,它没有,跳到else语句并重新发出消息“you do 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";
        }
    }
}

共有2个答案

艾浩广
2023-03-14

这将引发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();
    }
}
苏选
2023-03-14

您的问题是在迭代数组时不允许编辑它。像这样更改for循环以消除错误。另外,您正在使用if循环wronly。不要要求完整的条件为false,而只要求您希望在它之前编写的条件为false。

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";
        }
    }
}
 类似资料:
  • 我有一个假设,它有8个项目A-H,现在我想从中删除存储在int数组中的1,3,5位置项目。我该怎么做。 我正试着和你一起做这件事 但在数组的第一个被删除的项被更改之后,在下一次迭代中它删除了错误的元素或给出异常。

  • 我有一个程序,它从文件中获取输入,将文件中的每个单词保存为令牌,然后将每个令牌添加到数组列表中。 问题是arrayList出现了例如[“cat”,“dog”,“”,“”,“bird”],我不想在arrayList中出现空格。 总之,我的代码如下: 应该会删除空格,我已经通过从非文件数组列表中删除空格进行了测试。奇怪的是,如果我将其更改为,它将从ArrayList中删除cat。

  • 问题内容: 我正在遍历,并尝试将其值复制到中。问题在于它只能遍历一次。但是使用to 显示所有列的所有条目。以下是代码段- 复制到的唯一值是第1列。然后退出。但是我可以看到所有列的值。为什么? 问题答案: 如果我正确理解了您的问题,那么这里可能有两个问题: 是-我假设情况并非如此,就好像您在while循环中遇到异常并且什么也不会输出。 第二个问题是将从 随后的每一行中 获取1,2,3列,依此类推。

  • 问题内容: 我有以下Java代码,其中我试图将ArrayList复制到另一个ArrayList。 我希望“列表”数组采用以下格式: 但是从上面的代码中,“ list”数组输出看起来像这样: 我想您可能已经注意到了差异。我无法达到预期格式的结果。请建议我任何解决方案!提前致谢!! 问题答案: 然后,你需要的: 请注意,已更改为。在Java命名约定中,变量以小写字母开头。类以大写字母开头。

  • 但是,后来当我试图将整数添加到正确的“内部”数组列表中时,我无法将整数添加到ArrayList类型的位置中。我还得到一个索引超出界限的错误。 解决这个问题的方法是将整数强制转换为ArrayList类型,但这意味着我将在内部数组列表中添加一个数组列表,这不是我想要的。我想在正确的“内部”ArrayList中添加一个int。

  • 我的任务是实现一个蛮力算法来输出一些n的整数[1,2,…, n]的所有排列。但是,我似乎在将ArrayList对象添加到HashSet时遇到了一些问题: 我发现对“nextPer的连续调用确实找到了所有排列,但是我不明白当我将排列添加到HashSet“allPer的排列”时会发生什么。我在n=3的情况下运行时得到的输出是这样的: [[3, 2, 1, 1, 2, 1, 1, 3, 1, 2], [