无法在Java中将多个对象添加到列表

我有一个用Java编写的基本投资计算器,而且似乎无法将多个投资对象添加到ArrayList中。以下是一些基础知识(问题行在“如果添加401k”块中):

public static void main(String[] args) {
    input = new Scanner(System.in);
    ArrayList<Investment> invList = new ArrayList();

    int mainChoice = Util.mainmenu();

    while(3 != mainChoice) {

        //Add new investment
        if (1 == mainChoice) {
            int invChoice = Util.invMenu();

            //If adding a 401k
            if (1 == invChoice) {
                //gets info from user,these get passed as arguments when creating new object below

                //RetPlan401k extends the Investment superclass
                RetPlan401k inv = new RetPlan401k(invName,bal401k,contrb401k,ret401k,match,cap);
                invList.add(inv);
            }
            ...

从调试中我知道可以将任意投资对象添加到列表中,但是,当我要在每个对象上打印信息时(为简单起见,我们可以这样说:

(for int i = 0; i < invList.size(); ++i){
System.out.print("Investment name: " + invList.get(i).getName());
}) 

打印在vList.size()次添加的最新对象的名称。即我有invList.size()对象,它们都包含完全相同的信息。

这就像这里的数据结构101,但我有片刻,只是没有看到它。为什么不按输入方式打印每个投资对象?谢谢您的帮助。

wangjingjun2007 回答:无法在Java中将多个对象添加到列表

问题解决了……结果是我的对象类中有一些错误的“静态”关键字。每次都会得到你。谢谢大家。

本文链接:https://www.f2er.com/3162817.html

大家都在问