You don't need Collections.shuffle for this, but you should use it.
Here's how to do it similar to what you already have: I'm going to convert your String[]
to an ArrayList
because it has a remove method which removes an element from an index and returns that element. This just makes it easier than manipulating the String[]
Object, though you could do that instead. Then to create a pair, I'll draw a team from a random index, remove that team from the array, and draw again.
import java.util.Random;
public class FootyDraw {
public static void main(String... args) {
Random r = new Random();
String[] teams = {"Arsenal", "Chelsea", "Man United", "Liverpool"}; //array initializer
ArrayList<String> teamList = new ArrayList<>(Arrays.asList(teams)); //convert to a List to make this easier
if(teamList.size() % 2 != 0) teamList.add("none"); //ensure there are an even number of teams
while(!teamList.isEmpty()){ //while you have unmatched teams
String teamOne = teamList.remove(r.nextInt(teamList.size())); //pick the first team
String teamTwo = teamList.remove(r.nextInt(teamList.size())); //pick the second team
System.out.println(teamOne + " v " + teamTwo); //print the matchup
}
}
}
However, I would not do it this way because removing from an array copies the entire array to a new array without the item you removed, which has terrible performance. Collections.shuffel is better because it swaps random elements in the array, rather than copying them to a new array.
Thus, I would use Collections.shuffle to randomize the array, and then print out the matchup by continuous pairs in the array:
String[] teams = {"Arsenal", "Chelsea", "Man United", "Liverpool"}; //array initializer
ArrayList<String> teamList = new ArrayList<>(Arrays.asList(teams)); //convert to a List to make this easier
Collections.shuffle(teamList); //shuffle the teams
for(int i = 0; i < teamList.size(); i = i + 2){ //for each pair of teams
System.out.println(teamList.get(i) +" v "+ teamList.get(i+1)); //print the matchup
}