0

I'm making a Fixture Generator and need to generate random matching of items (teams) each time a button is clicked.

I m using a static String Array to hold the teams inputed by the user.

String s[]=new String[3];
s[0]="team1";
s[1]="team2";
s[2]="team3";

How can I shuffle this array?

Is it possible to get every possible combination of strings?

laffuste
  • 16,287
  • 8
  • 84
  • 91

1 Answers1

0

You could use something like this to generate the teams and shuffle them:

// Using a list.
final int teamCount = 10;
final List<String> teams = new ArrayList<>();
for (int teamIndex = 0; teamIndex < teamCount; teamIndex++)
    teams.add("team" + (teamIndex + 1));
Collections.shuffle(teams);
System.out.println("teams: " + teams);

// Using an array.
final int teamCount = 10;
final String[] teams = new String[teamCount];
for (int teamIndex = 0; teamIndex < teamCount; teamIndex++)
    teams[teamIndex] = "team" + (teamIndex + 1);
Collections.shuffle(Arrays.asList(teams));
System.out.println("teams: " + Arrays.toString(teams));
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28