Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exchange the position of two objects in a ArrayList?

I have a class called Player (with a lot of things on it).

And also, i have a ArrayList of Players with 20 players: List<Player> Players = new ArrayList<Player>();

for example, i want to exchange the player on the position 2 of the array with the player in the position 15 of the array.

Can someone give me the code to do this please? i don't know and i can't find the way on the documentation/google

thanks

like image 338
NullPointerException Avatar asked Aug 03 '11 23:08

NullPointerException


1 Answers

Try Collections.swap:

Collections.swap(Players, 2, 15);
like image 67
StriplingWarrior Avatar answered Sep 18 '22 00:09

StriplingWarrior