Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有一种方法可以让我从数组中随机生成字符串,而不必重复一个数组两次以上?_Java_Arrays_Math_Random_Duplicates - Fatal编程技术网

Java 有没有一种方法可以让我从数组中随机生成字符串,而不必重复一个数组两次以上?

Java 有没有一种方法可以让我从数组中随机生成字符串,而不必重复一个数组两次以上?,java,arrays,math,random,duplicates,Java,Arrays,Math,Random,Duplicates,目前正在制作一个程序,该程序将从两个不同的数组生成字符串列表,但现在它所做的只是重复生成所需次数的字符串,有时会生成多个重复的字符串 public class ListGenerator { public static void main(String[] args) { //number generator to determine which string to print Random g

目前正在制作一个程序,该程序将从两个不同的数组生成字符串列表,但现在它所做的只是重复生成所需次数的字符串,有时会生成多个重复的字符串

    public class ListGenerator
    {
          public static void main(String[] args)
          { 
           //number generator to determine which string to print
           Random generator = new Random();
           int rand;

           //counter to determine number of printed lines
           int counter = 0;

           //coin flip to say which array the string will come from
           Random coinFlip = new Random();
           int coin;

           String[] list1;
           list1 = new String[5]
           list1[0] = "Alpha"
           list1[1] = "Beta"
           list1[2] = "Charlie"
           list1[3] = "Delta"
           list1[4] = "Echo"

           String[] list2;
           list2 = new String[5]
           list2[0] = "Apple"
           list2[1] = "Pear"
           list2[2] = "Grape"
           list2[3] = "Banana"
           list2[4] = "Orange"

           for(counter = 0; counter < 15; counter++)
           {
                coin = coinFlip.nextInt(2)+1;

                if(coin == 1)
                {
                      rand = generator.nextInt(list1.length);
                      System.out.println(list1[rand]);
                }
                else if(coin == 2)
                {
                      rand = generator.nextInt(list2.length);
                      System.out.println(list2[rand]);
                }
           }
           }
    }
其中5个字符串生成了两次,但不超过两次,但在我的代码中,它可以生成任何一个字符串3、4、5次,等等


我知道我的代码不是组织得最好或最有效的,我只需要在这个复制问题上得到帮助

创建一个新的
数组列表
,其中包含20个值,其中
列表1
列表2
中的每个值添加两次

现在查看列表并获取前15个值

String[] list1 = { "Alpha", "Beta", "Charlie", "Delta", "Echo" };
String[] list2 = { "Apple", "Pear", "Grape", "Banana", "Orange" };

List<String> allTwice = new ArrayList<>(20);
allTwice.addAll(Arrays.asList(list1));
allTwice.addAll(Arrays.asList(list1));
allTwice.addAll(Arrays.asList(list2));
allTwice.addAll(Arrays.asList(list2));
Collections.shuffle(allTwice);
String[] result = allTwice.subList(0, 15).toArray(new String[15]);

for (String value : result)
    System.out.println(value);

顺便说一句,您在初始化过程中重复了五次
list2[0]
String[] list1 = { "Alpha", "Beta", "Charlie", "Delta", "Echo" };
String[] list2 = { "Apple", "Pear", "Grape", "Banana", "Orange" };

List<String> allTwice = new ArrayList<>(20);
allTwice.addAll(Arrays.asList(list1));
allTwice.addAll(Arrays.asList(list1));
allTwice.addAll(Arrays.asList(list2));
allTwice.addAll(Arrays.asList(list2));
Collections.shuffle(allTwice);
String[] result = allTwice.subList(0, 15).toArray(new String[15]);

for (String value : result)
    System.out.println(value);