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

调用showRiepilogo()方法无法显示ArrayList元素,求排查原因

Hey there! Let's figure out why your showRiepilogo() method isn't displaying the expense items from your voci ArrayList. Here are the most common issues and fixes to check:

Possible Causes & Solutions

1. You're not actually adding items to the voci collection

First things first: if you never populate the voci list with Voce objects, it'll be empty when you call showRiepilogo().

  • Make sure you have a method to add items to the list, like this:
    public void addVoce(Voce voce) {
        // Guard against null entries to avoid unexpected issues
        if (voce != null) {
            voci.add(voce);
        }
    }
    
  • Then, when using your RiepilogoMensile instance, remember to call this method to add actual expense items:
    RiepilogoMensile luglio2024 = new RiepilogoMensile();
    luglio2024.addVoce(new Voce("Supermercato", 180.50));
    luglio2024.addVoce(new Voce("Affitto", 950.00));
    

2. The showRiepilogo() implementation has a bug

Double-check how you're trying to display the voci list. It's easy to miss the loop that iterates through the collection.

  • Bad example (missing the loop to print items):
    public void showRiepilogo() {
        System.out.println("Riepilogo: " + mese + "/" + anno);
        // Oops! No code to loop through voci and print each item
    }
    
  • Fixed implementation with proper iteration:
    public void showRiepilogo() {
        System.out.println("=== Riepilogo Mensile " + mese + "/" + anno + " ===");
        if (voci.isEmpty()) {
            System.out.println("Nessuna voce di spesa registrata.");
            return;
        }
        // Loop through each Voce and print its details
        for (Voce voce : voci) {
            // Assuming Voce has getter methods for name and amount
            System.out.println("- " + voce.getNome() + ": €" + voce.getImporto());
        }
    }
    

3. The Voce class doesn't override toString() (if you're relying on it)

If your showRiepilogo() prints Voce objects directly (like System.out.println(voce);), Java will use the default toString() method which prints a hashcode instead of the actual data. Fix this by adding a custom toString() to your Voce class:

public class Voce {
    private String nome;
    private double importo;

    // Constructor, getters, setters...

    @Override
    public String toString() {
        return nome + ": €" + importo;
    }
}

4. Inheritance conflict with the parent Riepilogo class

Since RiepilogoMensile extends Riepilogo, check if the parent class also defines a voci ArrayList. If both classes have a private ArrayList<Voce> voci member, the subclass's version will shadow the parent's. This means any items added to the subclass's list won't be visible to methods in the parent (if showRiepilogo() is defined there).

  • Fix: Either mark the parent's voci as protected so the subclass can use it, or remove the duplicate voci definition in RiepilogoMensile and use the parent's collection instead.

内容的提问来源于stack exchange,提问作者Elias Mauro Antioco Murgia

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:18:32