mardi 3 mars 2015

How to pick a random item from a arraylist in java? [duplicate]


This question already has an answer here:




How can I pick a random item from a list of items in a array list, for example;



ArrayList<Integer> mylist= new ArrayList<Integer>();
mylist.add(19);
mylist.add(154);
mylist.add(112);
mylist.add(15);
mylist.add(112);


Currently, I am doing this but because I need to use this over and over again, is there a shorter way of do this?



Random random = new Random();
Integer randomInt = lista.get(rand.nextInt(lista.size()));




Aucun commentaire:

Enregistrer un commentaire