[英]How to print out unique random numbers from array without repeating
I need to simply print numbers with 0-9 from array in order without duplicating the numbers
in a random order我需要简单地从数组中按顺序打印 0-9 without duplicating the numbers
, without duplicating the numbers
以随机顺序without duplicating the numbers
Example: 0367458192示例:0367458192
So far, I have this but It prints out the numbers with spaces
, ,
and [
which I don't want.到目前为止,我有这个,但它打印出带有spaces
,
和[
我不想要的数字。 I feel like this is really simple but I just cant figure it out.我觉得这真的很简单,但我就是想不通。 Thanks谢谢
package garbage;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class ReversalGame {
public static void main(String[] args) {
Integer[] arr = new Integer[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
System.out.println(Arrays.toString(arr));
}
}
You can use the output of Arrays.toString
and then use String::replaceAll
to get rid of what you do not want您可以使用Arrays.toString
的输出,然后使用String::replaceAll
摆脱您不想要的东西
Integer[] arr = new Integer[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
String res = Arrays.toString(arr).replaceAll("\\[|\\]|,|\\s", "");
System.out.println(res);
output输出
3752486019
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.