我只是坚持在数组中获取一个随机字符串

时间:2013-12-31 18:42:25

标签: java arrays

我只是坚持在数组中获取随机字符串。

到目前为止,我有这个:

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

有没有人知道如何让我的数组中的每个字符串项打印一次而不重复?

(例如我想得到利物浦对阵曼联,然后是切尔西和阿森纳)。

5 个答案:

答案 0 :(得分:6)

List<String> shuffled = Arrays.asList(teams);
Collections.shuffle(shuffled);

答案 1 :(得分:6)

您可以随机播放数组,然后按新顺序打印每个元素:

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

答案 2 :(得分:4)

这样的事情

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

答案 3 :(得分:2)

您可以尝试改组阵列:

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

答案 4 :(得分:0)

您不需要Collections.shuffle,但您应该使用它。

以下是如何执行此操作类似于您已有的内容:我要将您的String[]转换为ArrayList,因为它有一个remove方法,它从索引中删除一个元素并返回元件。这只是比操纵String[]对象更容易,尽管你可以这样做。然后创建一对,我将从随机索引中绘制一个团队,从阵列中删除该团队,然后再次绘制。

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
        }
    }
}

但是,我不会这样做,因为从数组中删除会将整个数组复制到一个没有删除项目的新数组,这会带来糟糕的性能。 Collections.shuffel更好,因为它交换数组中的随机元素,而不是将它们复制到新数组。

因此,我会使用Collections.shuffle随机化数组,然后通过数组中的连续对打印出匹配:

    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
    }