How to choose a random item in the collection? I tried through toArray (), and then I chose and cast, there it was not possible to add Object [] to Player [].

  • In the garden of elder, in Kiev region uncle. What is the issue then? - rjhdby
  • Do you have a player array or collection? How did you try? why not take shuffle? - Alexey Shimansky
  • The collection looks something like this: Collection <? extends Player> players = ... - MrTrojan
  • one
    Similar question on en.SO: stackoverflow.com/questions/21092086/… - awesoon

1 answer 1

You can always find out the size of the collection:

It is necessary to get the index of a random element.

Random rnd = new Random(); int i = rnd.nextInt(myCollection.size()); player = (Player)(myCollection.toArray() [i]);