samedi 19 mars 2022

(Java) Made an ArrayList and I'm looking to randomize the names, numbers, and positions. How do I make them not repeat? [duplicate]

I made an array list with the following names, positions, and numbers.

ArrayList<Player> playerInfo = new ArrayList<Player>();

playerInfo.add(new Player("Wilson", 'F', 14));
playerInfo.add(new Player("Johnson", 'F', 22));
playerInfo.add(new Player("Terry", 'D', 27));
playerInfo.add(new Player("Price", 'G', 34));
playerInfo.add(new Player("Stamkos", 'F', 26));

I'm looking to make something that outputs a random name, position, and number. For example having it choose Wilson, G, 26.

I have the following to make it choose a random one of each but I don't know how to stop them from from duplicating (Positions can duplicate since there's only 3 options and I'm only showing the code on how it gets the name but the exact same code is used to get the number and position).

for(Player name:playerInfo)
    {
      Random genSurname = new Random();
      int randSurname = genSurname.nextInt(5);
      System.out.println(playerInfo.get(randSurname).getSurname());
    }

Any direction would be appreciated. Thanks!




Aucun commentaire:

Enregistrer un commentaire