【问题标题】:Give randomly Combination of Strings in TextFields each time on button click?每次单击按钮时在文本字段中随机组合字符串?
【发布时间】:2015-05-01 13:48:30
【问题描述】:

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

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

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

我怎样才能洗牌这个数组?

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

【问题讨论】:

  • 这里有一个类似的问题:stackoverflow.com/questions/1519736/…
  • Collections.shuffle(Arrays.asList(array));
  • @JB 您应该将其发布为答案,最好附上相关文档的链接。
  • 欢迎来到 Stack Overflow!

标签: java string swing


【解决方案1】:

您可以使用类似的方法来生成团队并对其进行洗牌:

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

【讨论】:

    猜你喜欢
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    相关资源
    最近更新 更多