import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class ShuffleList {
public static void main(String[] args) {
String [] file = {"1","2"};
long seed = 3;
ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(file));
for (int i=0; i<200; i++) {
Collections.shuffle(fileList, new Random(seed));
seed = seed +1;
System.out.println(seed + "," + fileList);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是200行[1,2],而不是随机的随机播放.事实上,所有种子<4000都是如此.为什么会这样?我尝试了3个元素的列表,种子从1到100使得列表看似随机.但是2个元素列表有什么问题?
我知道不建议捕获NPE,但是NPE是一个方法,其返回值是基于NPE来源的变量的布尔值.
代码是这样的:
public static boolean isStale() {
return a.get(); //a==null causing NPE
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我真的不知道如果没有a的信息将返回什么.我该怎么办?如果有的话,我应该扔什么样的例外?
编辑:
1.返回true或false将是不准确的,因为结果实际上取决于a内的信息.