所以,當我使用它來挑隨機事件的字符串,返回一個空:Java數組返回Null
public static List<String> pickNRandom(List<String> lst, int n) {
List<String> copy = new LinkedList<String>(lst);
Collections.shuffle(copy);
return copy.subList(0, n);
}
static List<String> randomP;
public List<String> items(){
List<String> teamList = new LinkedList<String>();
teamList.add("team1");
teamList.add("team2");
teamList.add("team3");
teamList.add("team4");
teamList.add("team5");
teamList.add("team6");
List<String> randomPicks = pickNRandom(teamList, 3);
randomPicks = randomP;
return randomPicks;
}
public static void Store() {
Random rand = new Random();
int people = rand.nextInt(50) + 1;
List<String> itemsIn = randomP;
System.out.println("People in store: "+people + "\nItems in store: "+itemsIn);
}
public static void main(String[] args) {
Store();
}
爲什麼返回null,我能做些什麼來解決這個問題?
是的,我一點也不清楚你在這裏要做什麼! –
爲什麼在返回'randomPicks'之前你指定'randomPicks = randomP;'? – 4castle
items()方法甚至沒有調用,npe發生在sysout – Zeromus