2015-05-01 25 views
0

我正在製作一個夾具生成器,並且每次點擊一個按鈕時都需要生成項目(團隊)的隨機匹配。每次點擊按鈕時,隨機給TextField中的字符串組合?

我使用靜態字符串數組來保存由用戶輸入的團隊。

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

我該如何洗這個數組?

是否有可能獲得每個可能的字符串組合?

+2

這裏有一個類似的問題:http://stackoverflow.com/questions/1519736/random-shuffling-of-an- array –

+0

'Collections.shuffle(Arrays.asList(array));' –

+0

@JB您應該將其作爲答案發布,最好帶有指向相關文檔的鏈接。 –

回答

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