Also, wenn ich diese verwenden, um einen String von zufälligen Ereignissen holen es ein Null zurückgibt:Java Array liefert 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();
}
Warum es null zurückkehrt und was kann ich tun, um dieses Problem beheben?
Ja, mir ist gar nicht klar, was Sie hier machen wollen! –
Warum weisen Sie 'randomPicks = randomP;' zu, bevor Sie 'randomPicks' zurückgeben? – 4castle
items() Methode wird nicht einmal aufgerufen, npe passiert bei sysout – Zeromus