每次按下按钮时,在TextFields中随机组合字符串组合?

时间:2015-05-01 13:48:30

标签: java string swing

我正在制作一个Fixture Generator,每次点击一个按钮时都需要生成项目(团队)的随机匹配。

我使用静态字符串数组来保存用户输入的团队。

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

我如何改变这个阵列?

是否可以获得所有可能的字符串组合?

1 个答案:

答案 0 :(得分:0)

你可以使用这样的东西来生成团队并将其改组:

// 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));