-2

I'm just stuck on getting a random strings in an array.

I have this so far:

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 
                String draw = teams[r.nextInt(teams.length)];
          }
}

Does anyone know how I can get each string item in my array to print out once without repeating?

(For instance I want to get for instance Liverpool v Man u and then Chelsea v Arsenal).

nachokk
  • 14,363
  • 4
  • 24
  • 53

5 Answers5

6
List<String> shuffled = Arrays.asList(teams);
Collections.shuffle(shuffled);
Alex Godofsky
  • 708
  • 4
  • 16
6

You can shuffle the array, then print each element in the new order:

public class FootyDraw { 

        public static void main(String[] args) { 

                String[] teams = {"Arsenal", "Chelsea", "Man United", "Liverpool"}; //array         initializer 
                List<String> list =  Arrays.asList(teams);
                Collections.shuffle(list);
                for (int i = 0; i < teams.length; i += 2) {
                    if(i + 1 < teams.length) System.out.println(teams[i] + " v " + teams[i + 1]);
                }
          }
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
4

How about something like this

public static void main(String[] args) {
    String[] teams = { "Arsenal", "Chelsea", "Man United", "Liverpool" }; // array
                                                                            // initializer
    java.util.List<String> al = new java.util.ArrayList<String>();
    for (String team : teams) {
        al.add(team);
    }
    java.util.Collections.shuffle(al);
    System.out.println(al);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

You could try shuffling the array:

public static void main(String args[])
{
    String[] teams = { "Arsenal", "Chelsea", "Man United", "Liverpool" }; //array initializer 
    shuffleArray(teams);
    for (String s : teams)
        System.out.println(s);
}

static void shuffleArray(String[] ar)
{
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        String a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

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
    }
Rick Hanlon II
  • 20,549
  • 7
  • 47
  • 53