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

如何在Java三维数组的单个坐标中存储多个字符串变量?

核心问题解决

你原本的思路是对的,但选错了存储结构:

  • int birthday[][]是整型数组,无法存储字符串列表,你不需要用三维数组,月份、日期本身就是数组的下标(天然为整型),只需要把二维数组的元素类型改为ArrayList<String>即可,每个位置对应当天的过生日人员列表。
  • 不要定义全局的namen列表存储所有名字,会导致所有日期的名字混杂,每个日期要对应独立的列表。
修正后可运行代码
import java.util.Scanner;
import java.util.ArrayList;

public class VerjaardagKalender {

    public static void optionChoice() {
        System.out.println("1 新增生日");
        System.out.println("2 删除生日");
        System.out.println("3 查看所有生日");
        System.out.println("4 退出程序");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 定义12个月31天的存储结构,每个位置存当天的生日人员列表
        ArrayList<String>[][] birthday = new ArrayList[12][31];
        // 初始化每个日期的空列表,避免空指针异常
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 31; j++) {
                birthday[i][j] = new ArrayList<>();
            }
        }

        System.out.println("生日日历 0.1");
        optionChoice();

        while (true) {
            System.out.println("请选择操作编号:1/2/3/4");
            String choice = scanner.nextLine();

            if (choice.equals("1")) {
                // 新增生日
                System.out.println("请输入过生日的人员姓名:");
                String name = scanner.nextLine();
                System.out.println("请输入生日日期(1-31):");
                int day = scanner.nextInt();
                System.out.println("请输入生日月份(1-12):");
                int month = scanner.nextInt();
                // 吃掉nextInt残留的换行符,避免后续nextLine读空
                scanner.nextLine();
                // 校验日期合法性
                if (month < 1 || month >12 || day <1 || day>31) {
                    System.out.println("输入的日期不合法,请重新操作");
                    continue;
                }
                // 把名字加到对应日期的列表里
                birthday[month-1][day-1].add(name);
                System.out.println("新增成功!");
            } else if (choice.equals("2")) {
                // 删除生日
                System.out.println("请输入要删除的人员姓名:");
                String name = scanner.nextLine();
                System.out.println("请输入该人员的生日日期(1-31):");
                int day = scanner.nextInt();
                System.out.println("请输入该人员的生日月份(1-12):");
                int month = scanner.nextInt();
                scanner.nextLine();
                if (month < 1 || month >12 || day <1 || day>31) {
                    System.out.println("输入的日期不合法,请重新操作");
                    continue;
                }
                // 从对应日期的列表里删除名字
                boolean isRemoved = birthday[month-1][day-1].remove(name);
                if (isRemoved) {
                    System.out.println("删除成功!");
                } else {
                    System.out.println("未查询到该日期下的对应人员,删除失败");
                }
            } else if (choice.equals("3")) {
                // 查看所有生日
                System.out.println("===== 所有生日列表 =====");
                for (int i = 0; i < 12; i++) {
                    for (int j = 0; j < 31; j++) {
                        ArrayList<String> nameList = birthday[i][j];
                        if (!nameList.isEmpty()) {
                            System.out.printf("%d月%d日过生日的人员:%s%n", i+1, j+1, String.join("、", nameList));
                        }
                    }
                }
            } else if (choice.equals("4")) {
                System.out.println("程序退出");
                scanner.close();
                break;
            } else {
                System.out.println("输入的编号不合法,请重新输入");
            }
        }
    }
}
补充说明
  • 你之前疑惑的「存储整型和字符串」需求不需要额外实现:月份和日期就是数组的下标,天然是整型,数组元素存储字符串列表即可,不存在类型混合存储的问题。
  • 代码里增加了简单的日期合法性校验,实际使用可以根据月份的实际天数(比如2月只有28/29天,4月只有30天)做更严格的校验。

内容的提问来源于stack exchange,提问作者Quincy van Deursen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 09:24:05