Answer the question
In order to leave comments, you need to log in
Don't know how to assign array values to another one?
Please explain, I have two arrays long[] id and String[] users,
The task is that I need to assign an ID to each user, so that later I can find it by it, thanks to
the User[] users field, which acts as a conditional base data.
In the class, write the
getUserNameById(long id) method - to get the username by its id
Answer the question
In order to leave comments, you need to log in
the logic of matching id and User is incomprehensible.
and so
HashMap <int,String>usersDict = new HashMap<int, String>();
i = 0;
for(String singleUser: users){
usersDict.put(id.get(i),singleUser);
i++;
}
Это создаст HashMap с ключами id - User.
Соответсвие будет по номеру элемента массива. Id(0) элемент массива, User(0) Элемент массива.
the User[] users field, which acts as a conditional database.
In the class, write the
getUserNameById(long id) method - to get the username, by its id
, there is only this
public User getUserNameById(long id) {
for (User user : users) {
if (id.equals(user.getId())) {
return user;
}
}
return null;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question