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

Java中loadVehicles方法返回空ArrayList问题求助

排查ArrayList返回空的问题

可能的原因及排查步骤

1. 外层if条件未触发(最常见)

先确认方法内的System.out.println(vehicles)是否真的输出了内容:

  • 在if (line.startsWith("[Vehicle]"))前添加打印语句:System.out.println("读取到行: '" + line + "'"),检查实际读取的行是否确实以[Vehicle]开头,有没有前置空格、不可见字符(如UTF-8 BOM)。
  • 如果行内容正确但startsWith返回false,可能是文件编码问题,可尝试指定编码读取:
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8))
    

2. 内部循环消耗了下一个Vehicle标记行

你的内部循环条件是(line = reader.readLine()) != null && !line.isEmpty(),如果Vehicle块之间没有空行,内部循环会读取下一个[Vehicle]行并跳过(进入default分支),导致外层循环无法捕获该标记。例如文件结构:

[Vehicle]
MakeModel: Sedan
[Vehicle]
MakeModel: SUV

此时第一个Vehicle处理完后,内部循环会读取到[Vehicle]行(不为空),继续执行直到文件结束,第二个Vehicle永远不会被外层的if条件处理。

修复方案:重构内部循环逻辑,遇到新的块标记时停止读取当前Vehicle的属性:

public static ArrayList<Vehicle> loadVehicles(String filePath) {
    ArrayList<Vehicle> vehicles = new ArrayList<>();
    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
        String line = reader.readLine();
        while (line != null) {
            if (line.startsWith("[Vehicle]")) {
                String makeModel = null;
                String quality = null;
                int seats = 0;
                int rentalPrice = 0;
                boolean available = false;

                boolean readingAttributes = true;
                while (readingAttributes) {
                    line = reader.readLine();
                    // 遇到空行、新块标记或文件结束时,停止读取当前属性
                    if (line == null || line.isEmpty() || line.startsWith("[")) {
                        readingAttributes = false;
                    } else {
                        String[] parts = line.split(": ");
                        if (parts.length >= 2) { // 避免格式错误导致数组越界
                            switch (parts[0]) {
                                case "MakeModel":
                                    makeModel = parts[1];
                                    break;
                                case "Quality":
                                    quality = parts[1];
                                    break;
                                case "Seats":
                                    seats = Integer.parseInt(parts[1]);
                                    break;
                                case "RentalPrice":
                                    rentalPrice = Integer.parseInt(parts[1]);
                                    break;
                                case "Available":
                                    available = Boolean.parseBoolean(parts[1]);
                                    break;
                            }
                        }
                    }
                }

                Vehicle v = new Vehicle(makeModel, quality, seats, rentalPrice, available);
                vehicles.add(v);
                System.out.println(vehicles);
                System.out.println(v);
            } else {
                // 跳过非Vehicle标记的行
                line = reader.readLine();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return vehicles;
}

3. 异常导致代码提前退出

如果属性行格式错误(比如Seats: abc),Integer.parseInt会抛出NumberFormatException,此时代码会跳转到catch块,当前Vehicle的添加操作不会执行。

排查方式:在parse操作前增加校验,避免异常中断流程:

// 处理Seats时增加校验
case "Seats":
    try {
        seats = Integer.parseInt(parts[1]);
    } catch (NumberFormatException e) {
        System.err.println("Seats格式错误: " + parts[1]);
    }
    break;

4. 调用问题

如果方法内的System.out.println(vehicles)有内容,但调用后返回的列表为空:

  • 确认你调用的是同一个loadVehicles方法,没有重载的同名方法导致调用错误。
  • 检查调用后是否有代码修改了返回的列表(如clear()、removeAll())。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 14:47:11