无法从列表中接收多个不同的随机字符串(Java)。

huangapple go评论79阅读模式
英文:

Not able to receive multiple different random Strings from list in Java

问题

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TestingOutputs {
    public static void main(String[] args) {
        boolean forever = true, one = true, two = true, three = true;
        List<String> outputsList = new ArrayList<String>();
        outputsList.add("One");
        outputsList.add("Two");
        outputsList.add("Three");

        while (forever) {
            String outputsRandom = outputsList.get(new Random().nextInt(outputsList.size()));
            while (three) {
                if (outputsRandom.equals("Three")) {
                    System.out.println("This is three");
                    three = false;
                }
                while (two) {
                    if (outputsRandom.equals("Two")) {
                        System.out.println("This is two");
                        two = false;
                    }
                }
                while (one) {
                    if (outputsRandom.equals("One")) {
                        System.out.println("This is one");
                        two = false;
                    }
                }
            }
        }
    }
}
英文:

I want to code it so that the output of my code to output all of the Strings in a list in a random order, but not repeat and of them. I am hoping to do this with more than three variables, but to keep my question short, I only have three in this example

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TestingOutputs {
    public static void main(String[] args) {
        boolean forever = true, one = true, two = true, three = true;
        List&lt;String&gt; outputsList = new ArrayList&lt;String&gt;();
        outputsList.add(&quot;One&quot;);
        outputsList.add(&quot;Two&quot;);
        outputsList.add(&quot;Three&quot;);

        while(forever){
            String outputsRandom = outputsList.get(new Random().nextInt(outputsList.size()));
            while(three) {
                if(outputsRandom.equals(&quot;Three&quot;)) {
                    System.out.println(&quot;This is three&quot;);
                    three = false;
                }
            while(two) {
                if(outputsRandom.equals(&quot;Two&quot;)) {
                    System.out.println(&quot;This is two&quot;);
                    two = false;
                }
            }
            while(one) {
                if(outputsRandom.equals(&quot;One&quot;)) {
                    System.out.println(&quot;This is one&quot;);
                    two = false;
                }
            }
            }
        }
        }

    }

答案1

得分: 1

首先创建一个从0到字符串数量的数字列表。

List<Integer> range = IntStream.rangeClosed(0, outputsList.size() - 1)
    .boxed().collect(Collectors.toList());

然后对创建的列表进行洗牌,并在for-each循环中获取元素:

Collections.shuffle(range);
for (Integer i : range) {
  outputsList.get(i.intValue());
}

这些数字将是随机且不重复的。希望这能解决您的问题。

英文:

First create a list of numbers starting from 0 to the numbers of strings.

List&lt;Integer&gt; range = IntStream.rangeClosed(0, outputsList.size() - 1)
    .boxed().collect(Collectors.toList());

The shuffle the created list and get elements in a for-each loop.:

Collections.shuffle(range);
for(Integer i:range)
{
  outputsList.get(i.intValue());
}

The numbers would be random and non-repeating.
Hope this solves your problem.

答案2

得分: 0

以下是翻译好的部分:

一个可能的解决方案是克隆您的ArrayList,随机输出所选项目,然后将其删除。
在代码中,它看起来像这样:

import java.util.ArrayList;

public class Example {
  public static ArrayList<String> createList() {
    ArrayList<String> listTemp = new ArrayList<String>();
    listTemp.add("one");
    listTemp.add("two");
    listTemp.add("three");
    
    return listTemp;
  }
  
  public static void main(String[] args) {
    ArrayList<String> listOutputs = createList();
    
    // Clone to not alter original list
    ArrayList<String> listTemp = (ArrayList<String>) listOutputs.clone();
    
    while (listTemp.size() != 0) {
      int index = (int) (Math.random() * listTemp.size());
      System.out.println("Selected element: '" + listTemp.get(index) + "'");
      listTemp.remove(index);
    }
  }
}

然而,Vishal的解决方案是一种更清晰和更高效的方式,尤其是在处理较大列表时。

英文:

One possible solution is to clone your ArrayList, and randomly output the selected item, and then remove it.
In code, it would look like this:

import java.util.ArrayList;

public class Example {
  public static ArrayList&lt;String&gt; createList() {
    ArrayList&lt;String&gt; listTemp = new ArrayList&lt;String&gt;();
    listTemp.add(&quot;one&quot;);
    listTemp.add(&quot;two&quot;);
    listTemp.add(&quot;three&quot;);
    
    return listTemp;
  }
  
  public static void main(String[] args) {
    ArrayList&lt;String&gt; listOutputs = createList();
    
    // Clone to not alter original list
    ArrayList&lt;String&gt; listTemp = (ArrayList&lt;String&gt;) listOutputs.clone();
    
    while (listTemp.size() != 0) {
      int index = (int) (Math.random() * listTemp.size());
      System.out.println(&quot;Selected element: &#39;&quot; + listTemp.get(index) + &quot;&#39;&quot;);
      listTemp.remove(index);
    }
  }
}

However, Vishal's solution is by far a cleaner and more efficient way, especially with larger lists.

huangapple
  • 本文由 发表于 2020年9月4日 07:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63733077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定