jeudi 23 mars 2017

In java: Troubling generating a random order from an ArrayList

total newbie here. I've written a program in java to help randomize the order of bands for a concert I'm organizing. I'm having trouble getting the code to work. The output I get terminates after printing three strings instead of four, often repeats strings (which I don't want) and terminates after the third string with the following error:

"java.lang.IllegalArgumentException: bound must be positive"

Can anyone help troubleshoot my code?

public class BandRandomizer
{
public static void main(String[] args) 
{
    ArrayList<String> bands = new ArrayList<>();
    bands.add("Band A");
    bands.add("Band B");
    bands.add("Band C");
    bands.add("Band D");

    Random gen = new Random();
    int index = 0;

    for (int i = 3; i >= 0; i--)
    {
        index = gen.nextInt(i);
        System.out.println(bands.get(index));
        bands.remove(i);    
    }



}

}




Aucun commentaire:

Enregistrer un commentaire