且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中的for循环中创建几个新对象

更新时间:2022-06-20 02:58:05

你因为你创建一个新的 Bill 并且永远不会将它们保存在任何地方。我相信你想把它们添加到你的 ArrayList

You're overriding them because you create a new Bill on each loop and never save them off anywhere. I believe you want to add them to your ArrayList:

首先,你应该为你的 ArrayList :

ArrayList<Bill> ary = new ArrayList<Bill>();

然后,在您收到用户输入是否添加新 Bill ,您应该将当前的一个添加到此列表中:

Then, before you get the input from the user on whether or not to add a new Bill, you should add the current one to this list:

...
System.out.println("user Last name:");
bill1.setLname(input.nextLine());
ary.add(bill1);
...