mercredi 6 septembre 2017

Populating ArrayList in Java with Random Ints

So, I'm a little stuck on a line of code. I need to populate an integer ArrayList with a max size of 4 (indexes 0-3) with randomly generated numbers 1-9. I tweaked with it for a while, and managed to get code that compiled, but it returned -1 for every index.

I think that the ArrayList isn't being populated correctly, but I'm not sure how to go about it. Help or tips would be greatly appreciated.

public class NumberList 
{
    private ArrayList<Integer> tile ; 
    private static Random random = new Random();

    public NumberList() 
    {   
        ArrayList<Integer> tile = new ArrayList<Integer>();

        for (int i = 0; i < 4; i++)
        {
            tile.add(random.nextInt(10) + 1);
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire