初始化的ArrayList未显示内容,Java技术问题求助
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:
Menu branch logic isn’t triggering the display code
Double-check yourswitchorif-elseblock for option 3. It’s easy to accidentally skip the display method call, or place abreakin 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.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
nullinstead 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.- Forgetting to call
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 ofi < buildings.size()will throw anIndexOutOfBoundsException. 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 overriddentoString(), you’ll get weird default output (likeBuilding@123456), but you’d still see something. If you see nothing at all, the above issues are more likely.
- Incorrect loop conditions: Using
Input handling is causing unexpected behavior
If you’re usingScanner, callingnextInt()leaves a newline character in the input buffer. If your menu loop usesnextLine()afterward, it might read an empty string and re-display the menu before executing your display logic. Fix this by addingscanner.nextLine()afternextInt()to consume the newline.
Quick Debug Steps to Narrow It Down
- Confirm you’re actually entering the case 3 branch with a debug print.
- Verify your ArrayList has data by checking its size.
- Step through the display method line by line (or add more debug prints) to see where it’s failing.
内容的提问来源于stack exchange,提问作者Dio Tsolakou

