英文:
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<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;
}
}
}
}
}
}
答案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<Integer> 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<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);
}
}
}
However, Vishal's solution is by far a cleaner and more efficient way, especially with larger lists.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论