Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);


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);


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?
Posted
Updated 31-Oct-18 1:36am
v2

1 solution

Why is it not possible? It is quite valid to do this:
Java
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));
}
 
Share this answer
 
Comments
Member 14039072 31-Oct-18 6:44am    
Thanks, but how can I make the "indentifier" of the object to be the username that is filled in? Because later I want be able to easily delete an object by the username.
Richard MacCutchan 31-Oct-18 6:51am    
You do not need to do that, you just search the arraylist for the specific username and delete the entry. Have a look at ArrayList (Java Platform SE 8 )[^] for more information. Alternatively use one of the keyed collections such as Dictionary (Java Platform SE 8 )[^].
CPallini 31-Oct-18 8:19am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900