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

初始化的ArrayList未显示内容,Java技术问题求助

Troubleshooting Your "Display Buildings" Menu Issue

Hey there! I totally get the frustration when a feature that worked in a smaller lab breaks in a bigger project—let’s walk through the most likely culprits here, since you mentioned you just need help debugging, not completing the assignment.

Common Issues to Check

Let’s break down why selecting option 3 might only loop back to the menu without showing your buildings:

  1. Menu branch logic isn’t triggering the display code
    Double-check your switch or if-else block for option 3. It’s easy to accidentally skip the display method call, or place a break in the wrong spot. For example:

    switch(userChoice) {
        case 1:
            addBuilding();
            break;
        case 3:
            // Did you forget to call your display method here?
            // displayBuildings(); <-- This line might be missing!
            break;
        default:
            System.out.println("Invalid choice");
    }
    

    Quick test: Add a debug print like System.out.println("Option 3 selected!") at the start of the case 3 block. If you don’t see this message when you choose 3, the issue is in your menu selection handling.

  2. Your ArrayList has no data (even though you initialized it)
    Initializing the list (ArrayList<Building> buildings = new ArrayList<>();) only creates an empty container. You need to make sure your "add building" function is actually adding objects to it. Check for:

    • Forgetting to call buildings.add(newBuilding); after creating a Building object
    • Accidentally adding null instead of a valid Building instance
    • A bug in your input parsing that’s creating incomplete/empty Building objects

    Debug tip: In your display method, first print the list size: System.out.println("Total buildings: " + buildings.size()); If it returns 0, your add function is the problem.

  3. The display method itself has bugs
    Even if you’re entering the right branch and have data in the list, the display logic might be broken:

    • Incorrect loop conditions: Using i <= buildings.size() instead of i < buildings.size() will throw an IndexOutOfBoundsException. If your code has a try-catch swallowing this error, the method will fail silently and return to the menu.
    • Missing output statements: You might be iterating over the list but forgetting to print each building. For example:
      public void displayBuildings() {
          for(Building b : buildings) {
              // Oops! No System.out.println(b); here
          }
      }
      
    • Building class lacks a proper toString() method: If you’re printing the Building objects directly but haven’t overridden toString(), you’ll get weird default output (like Building@123456), but you’d still see something. If you see nothing at all, the above issues are more likely.
  4. Input handling is causing unexpected behavior
    If you’re using Scanner, calling nextInt() leaves a newline character in the input buffer. If your menu loop uses nextLine() afterward, it might read an empty string and re-display the menu before executing your display logic. Fix this by adding scanner.nextLine() after nextInt() to consume the newline.

Quick Debug Steps to Narrow It Down

  1. Confirm you’re actually entering the case 3 branch with a debug print.
  2. Verify your ArrayList has data by checking its size.
  3. Step through the display method line by line (or add more debug prints) to see where it’s failing.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:48:54