如何从表单中向一个arraylist添加多个对象? [英] How do I add multiple object to an arraylist from a form?
问题描述
我有一个名为User的类和一个arrayList
I have a class called User and a arrayList
ArrayList<User> users = new ArrayList<>();
我已经在这样的一个用户中添加:
I have already put in one user like this:
User newUser = new User("sunn", "1998");
User.users.add(newUser);
但是我会从你可以填写的表格中获得下一个用户出。
But then I will get the next users from a form you can fill out.
User newUser = new User(username, year);
User.users.add(newUser);
无法使用标识符newUser每次,但如何为未知数量的用户创建标识符?我真的希望标识符与用户名相同,但我该怎么做?
我尝试过的事情:
It is not possible to use the identifier newUser everytime, but how can I make a identifier to a unknow amount of users? I would really like to have the identifier as the same as the username, but how do I do this?
What I have tried:
User newUser = new User(username, year);
User.users.add(newUser);
无法使用标识符newUser每次,但如何为未知数量的用户创建标识符?我真的希望标识符与用户名相同,但我该怎么做?
It is not possible to use the identifier newUser everytime, but how can I make a identifier to a unknow amount of users? I would really like to have the identifier as the same as the username, but how do I do this?
推荐答案
为什么不可能?这样做非常有效:
Why is it not possible? It is quite valid to do this:
User newUser;
while (some condition) {
newUser = new User(username, year);
User.users.add(newUser);
}
// or you could do
while (some condition) {
User.users.add(new User(username, year));
}
这篇关于如何从表单中向一个arraylist添加多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!