S
S
Salmeg Zhambalova2019-02-04 09:23:47
Java
Salmeg Zhambalova, 2019-02-04 09:23:47

How to raise an element in a collection?

Question: There is a collection of class objects, you can get values ​​through getters.
How can I raise a specific element of the collection to the very top? For example, we have this:
456 adminX
935 test
864 user
We need to elevate the test user so that it turns out like this:
935 test
456 adminX
864 user

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-02-04
@selmeg

For ArrayList:

int i = 0;
for (; i < players.size(); i++) {
    if (players.get(i).getId() == 935)
        break;
}
players.set(0, players.set(i, players.get(0)));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question