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

Java如何向ArrayList添加元素并展示,出现索引越界异常如何解决

问题原因分析
  • 核心问题是存储学生数据的4个ArrayList被定义为Studentt()方法的局部变量:每次调用Studentt()方法时都会重新初始化这4个列表,之前添加的所有数据都会被清空。比如你第一次操作选择1添加了学生,选择重复操作再次调用Studentt()后,所有列表都会被重置为空列表,此时选择2查询任意索引都会触发索引越界,对应你报错里的length 0描述。
  • 次要问题1:end()方法中重新创建了Scanner实例且使用try-with-resources语法,执行完后会自动关闭Scanner,同时会把关联的System.in输入流关闭,后续操作再调用类中全局的scan对象时会无法读取输入。
  • 次要问题2:ArrayList的索引从0开始计数,就算数据存储正常,如果你输入1查询第一个录入的学生,也会触发索引越界。
修复方案

把4个ArrayList提到类的成员变量位置,同时去掉end()方法中多余的Scanner创建逻辑即可,参考调整后的代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Student {
    Scanner scan = new Scanner(System.in);
    String name_s, surname_s;
    int age_s, number_s;
    int choice;
    int index;
    // 将列表改为类成员变量,不会每次调用Studentt就重置
    ArrayList<String> name = new ArrayList<String>();
    ArrayList<String> surname = new ArrayList<String>();
    ArrayList<Integer> age = new ArrayList<Integer>();
    ArrayList<Integer> number = new ArrayList<Integer>();

    public void Studentt() {
        System.out.println("To add student:1\nTo see student:2");
        this.choice = Integer.parseInt(scan.nextLine());

        if (choice == 1) {
            System.out.println("Name:");
            this.name_s = scan.nextLine();
            name.add(name_s);
            System.out.println("Surname:");
            this.surname_s = scan.nextLine();
            surname.add(surname_s);
            System.out.println("Age:");
            this.age_s = Integer.parseInt(scan.nextLine());
            age.add(age_s);
            System.out.println("Number");
            this.number_s = Integer.parseInt(scan.nextLine());
            number.add(number_s);
            end();
        } else if (choice == 2) {
            System.out.println("Index of the student you want to see (start from 0):");
            this.index = Integer.parseInt(scan.nextLine());
            // 增加索引校验避免非法输入触发越界
            if (index < 0 || index >= name.size()) {
                System.out.println("输入索引无效,当前录入学生总数:" + name.size());
            } else {
                System.out.println(name.get(index));
                System.out.println(surname.get(index));
                System.out.println(age.get(index));
                System.out.println(number.get(index));
                System.out.println("Done.");
            }
            end();
        } else {
            System.out.println("Wrong choice.");
            Studentt();
        }
    }

    public void end() {
        int menu;
        System.out.println("To repeat:1 \nExit:2");
        // 复用全局的scan对象,不要重复创建也不要提前关闭
        menu = Integer.parseInt(scan.nextLine());
        if (menu == 1) {
            Studentt();
        } else if (menu == 2) {
            System.out.println("Done.");
            // 程序退出前再关闭Scanner
            scan.close();
        } else {
            System.out.println("Wrong choice.");
            end();
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 03:45:01