E
E
Eban2019-10-01 16:15:48
Java
Eban, 2019-10-01 16:15:48

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

2 answer(s)
D
DDwrt100, 2019-10-01
@DDwrt100

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) Элемент массива.

C
Cheypnow, 2019-10-01
@Cheypnow

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

According to this description, you need something like 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 question

Ask a Question

731 491 924 answers to any question