use*_*959 2 java random arraylist
我在java中有以下代码.在这里,我试图看到一个数组的随机组合.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class Dummy {
public static void main(String args[]) throws Exception {
Random rand = new Random();
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 1, 0, 1, 0, 1 };
Arrays.sort(a);
Arrays.sort(b);
int x = a.length * b.length;
System.out.println(x);
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 25; i++) {
System.out.println("random of i is" + a[rand.nextInt(i)]
+ "and j is " + b[rand.nextInt(i)]);
}
System.out.println(list);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我得到以下错误.
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at Dummy.main(Dummy.java:20)
Run Code Online (Sandbox Code Playgroud)
请让我知道如何解决它.根据基于相似问题的其他一些帖子,我发现只有正数才会给出随机数,我想知道我怎样才能给出负数.
此外,我想知道通过延续[]和b []可以获得多少组合.
谢谢
你error
是因为你传球0 rand.nextInt(i)
.
rand.nextInt() expects positive integer greater that 0.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
116 次 |
最近记录: |