You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java LinkedList移除游戏时索引越界异常原因咨询

LinkedList移除游戏时索引越界异常的解决方法

我是编程新手,正在学习Java。尝试从LinkedList中移除游戏时抛出了索引越界异常,代码如下:

import java.util.LinkedList;

public class GamesChallenge {

    private static LinkedList<Game> gameList = new LinkedList<>();

    public static void main(String[] args){

        //adding the games to the LinkedList

        addGame("Call of Duty", 300);
        addGame("Last day on Earth", 500);
        addGame("Fall Guys", 3000);
        System.out.println(gameList);

        //try to find the game in the LinkedList
        findGame("Fall Guys");

        //try to remove a game
        removeGame("Fall Guys");
        System.out.println(gameList);

    }

    public static void addGame(String name, int memory){
        // check if the song is in the linkedList
        Game game = new Game(name, memory);
        while(findGame(name) == false){
            //if the game does not exist then the game is added
            gameList.add(game);
            break;
        }

    }

    public static boolean findGame(String name){
        // check if the game is in the LinkedList
        if(gameList.indexOf(gameList.contains(name)) >= 0){
            //if the index is bigger or equal to 0 (the game is in the LinkedList) then it will return true
            return true;
        }
        //else it will return false
        else return false;
    }

 //here is where I have the problem
 
 public static void removeGame(String name){
     gameList.remove(gameList.indexOf(gameList.contains(name)));
 
 }
}


class Game {

    private String name;
    private int memory;

    public Game(String name, int memory){
        this.name = name;
        this.memory = memory;

    }

    @Override
    public String toString(){
        return name + ":" + memory;
    }

    public String getName() {
        return name;
    }

    public int getMemory() {
        return memory;
    }
}

核心问题分析

  • contains与indexOf的误用:gameList.contains(name)拿字符串和Game对象比较,由于Game未重写equals方法,默认用对象地址判断,永远返回false;同时indexOf需要传入Game类型参数,传入布尔值会导致返回-1,调用remove(-1)直接触发IndexOutOfBoundsException。
  • findGame逻辑完全错误:gameList.indexOf(gameList.contains(name))的参数类型不匹配,根本无法正确判断游戏是否存在。
  • addGame冗余循环:while里直接break,和if判断效果一致,且因findGame错误会导致重复添加。

修正方案

1. 重写Game类的equals和hashCode方法

LinkedList的contains、indexOf等方法依赖equals判断对象匹配,需根据游戏名重写:

import java.util.Objects;

// 在Game类中添加
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Game game = (Game) o;
    return Objects.equals(name, game.name);
}

@Override
public int hashCode() {
    return Objects.hash(name);
}

2. 修正findGame方法

通过遍历列表匹配游戏名:

public static boolean findGame(String name) {
    for (Game game : gameList) {
        if (game.getName().equals(name)) {
            return true;
        }
    }
    return false;
}

3. 简化addGame方法

直接判断后添加,避免重复:

public static void addGame(String name, int memory) {
    if (!findGame(name)) {
        gameList.add(new Game(name, memory));
    }
}

4. 修正removeGame方法

用removeIf直接移除匹配的游戏:

public static void removeGame(String name) {
    gameList.removeIf(game -> game.getName().equals(name));
}

完整修正代码

import java.util.LinkedList;
import java.util.Objects;

public class GamesChallenge {

    private static LinkedList<Game> gameList = new LinkedList<>();

    public static void main(String[] args) {
        addGame("Call of Duty", 300);
        addGame("Last day on Earth", 500);
        addGame("Fall Guys", 3000);
        System.out.println(gameList);

        System.out.println("Fall Guys 是否存在:" + findGame("Fall Guys"));

        removeGame("Fall Guys");
        System.out.println(gameList);
    }

    public static void addGame(String name, int memory) {
        if (!findGame(name)) {
            gameList.add(new Game(name, memory));
        }
    }

    public static boolean findGame(String name) {
        for (Game game : gameList) {
            if (game.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

    public static void removeGame(String name) {
        gameList.removeIf(game -> game.getName().equals(name));
    }
}

class Game {
    private String name;
    private int memory;

    public Game(String name, int memory) {
        this.name = name;
        this.memory = memory;
    }

    @Override
    public String toString() {
        return name + ":" + memory;
    }

    public String getName() {
        return name;
    }

    public int getMemory() {
        return memory;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Game game = (Game) o;
        return Objects.equals(name, game.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

内容的提问来源于stack exchange,提问作者MsLaus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:48:27