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

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

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

问题

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. public class TestingOutputs {
  5. public static void main(String[] args) {
  6. boolean forever = true, one = true, two = true, three = true;
  7. List<String> outputsList = new ArrayList<String>();
  8. outputsList.add("One");
  9. outputsList.add("Two");
  10. outputsList.add("Three");
  11. while (forever) {
  12. String outputsRandom = outputsList.get(new Random().nextInt(outputsList.size()));
  13. while (three) {
  14. if (outputsRandom.equals("Three")) {
  15. System.out.println("This is three");
  16. three = false;
  17. }
  18. while (two) {
  19. if (outputsRandom.equals("Two")) {
  20. System.out.println("This is two");
  21. two = false;
  22. }
  23. }
  24. while (one) {
  25. if (outputsRandom.equals("One")) {
  26. System.out.println("This is one");
  27. two = false;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
英文:

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

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4. public class TestingOutputs {
  5. public static void main(String[] args) {
  6. boolean forever = true, one = true, two = true, three = true;
  7. List&lt;String&gt; outputsList = new ArrayList&lt;String&gt;();
  8. outputsList.add(&quot;One&quot;);
  9. outputsList.add(&quot;Two&quot;);
  10. outputsList.add(&quot;Three&quot;);
  11. while(forever){
  12. String outputsRandom = outputsList.get(new Random().nextInt(outputsList.size()));
  13. while(three) {
  14. if(outputsRandom.equals(&quot;Three&quot;)) {
  15. System.out.println(&quot;This is three&quot;);
  16. three = false;
  17. }
  18. while(two) {
  19. if(outputsRandom.equals(&quot;Two&quot;)) {
  20. System.out.println(&quot;This is two&quot;);
  21. two = false;
  22. }
  23. }
  24. while(one) {
  25. if(outputsRandom.equals(&quot;One&quot;)) {
  26. System.out.println(&quot;This is one&quot;);
  27. two = false;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }

答案1

得分: 1

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

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

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

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

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

英文:

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

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

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

  1. Collections.shuffle(range);
  2. for(Integer i:range)
  3. {
  4. outputsList.get(i.intValue());
  5. }

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

答案2

得分: 0

以下是翻译好的部分:

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

  1. import java.util.ArrayList;
  2. public class Example {
  3. public static ArrayList<String> createList() {
  4. ArrayList<String> listTemp = new ArrayList<String>();
  5. listTemp.add("one");
  6. listTemp.add("two");
  7. listTemp.add("three");
  8. return listTemp;
  9. }
  10. public static void main(String[] args) {
  11. ArrayList<String> listOutputs = createList();
  12. // Clone to not alter original list
  13. ArrayList<String> listTemp = (ArrayList<String>) listOutputs.clone();
  14. while (listTemp.size() != 0) {
  15. int index = (int) (Math.random() * listTemp.size());
  16. System.out.println("Selected element: '" + listTemp.get(index) + "'");
  17. listTemp.remove(index);
  18. }
  19. }
  20. }

然而,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:

  1. import java.util.ArrayList;
  2. public class Example {
  3. public static ArrayList&lt;String&gt; createList() {
  4. ArrayList&lt;String&gt; listTemp = new ArrayList&lt;String&gt;();
  5. listTemp.add(&quot;one&quot;);
  6. listTemp.add(&quot;two&quot;);
  7. listTemp.add(&quot;three&quot;);
  8. return listTemp;
  9. }
  10. public static void main(String[] args) {
  11. ArrayList&lt;String&gt; listOutputs = createList();
  12. // Clone to not alter original list
  13. ArrayList&lt;String&gt; listTemp = (ArrayList&lt;String&gt;) listOutputs.clone();
  14. while (listTemp.size() != 0) {
  15. int index = (int) (Math.random() * listTemp.size());
  16. System.out.println(&quot;Selected element: &#39;&quot; + listTemp.get(index) + &quot;&#39;&quot;);
  17. listTemp.remove(index);
  18. }
  19. }
  20. }

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:

确定