jeudi 8 juin 2017

Select multiple/random elements from an arraylist

How would I select random elements from an array list, except for one element. Here's my arraylist:

ArrayList <String> provinces = new ArrayList();

 Collections.addAll(provinces, "New Brunswick", "Saskatchewan", "Ontario", "Nova Scotia", "Quebec", "Alberta");

For this example, I would like to select at random the other elements, except Saskatchewan.

I've tried doing:

 for(int i == provinces.get (0); i < provinces.get(1); i > provinces.get(2); i < provinces.get(5)) {
     int getPossibleAnswers = (int) Math.floor(Math.random()*i);               
     String displayPossibleAnswers = provinces.get(getPossibleAnswers);                
     outputAnswers.append(displayPossibleAnswers + "\n");
     }

Obviously this code doesn't work, and I don't know what to do. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire