Arcadian
Arcadian

Reputation: 1373

Adding arrays to ArrayList

I'm trying to produce a kind of lucky dip lotto number generator. At the moment, the code will produce the desired results but I'd really like to have the arrays in an arraylist. Here's my code so far:

Ticket Class:

public class Ticket
{
    private int numberOfLines;

    public void displayTicket(int numberOfLines)
    {
        Numbers numbersClass = new Numbers();

        System.out.println("***************************************");
        System.out.println("**                                   **");
        System.out.println("**           Lotto Ticket            **");
        System.out.println("**                                   **");
        System.out.println("***************************************");
        System.out.println("**                                   **");

        for(int i = 0; i < numberOfLines; i++)
        {
            numbersClass.populateArray();
            numbersClass.ticket();
        }

        System.out.println("**                                   **");
        System.out.println("***************************************");

    }
}

Numbers Class:

public class Numbers
{
    // Fields
    private int[] numberLine;
    private int randomNumber;
    private Random randomGen;


    // Constructor : Initialise number array
    public Numbers()
    {
        numberLine = new int[6];
        randomNumber = 0;
        randomGen = new Random();
    }

    // Method : Generate 6 random numbers in a range 1 to 49
     public void populateArray()
    {
        List<Integer> pool = new ArrayList<Integer>();
        for (int i = 0; i < 49; ++i)
        {
            pool.add(i + 1);
        }
        for (int i = 0; i < 6; ++i)
        {
            randomNumber = randomGen.nextInt(pool.size());
            numberLine[i] = pool.get(randomNumber);
            pool.remove(randomNumber);
        }
    }

    // Method : Sort 6 numbers in ascending order  
    private int[] sortArray()
    {
        Arrays.sort(numberLine);
        return numberLine;
    }

    // Method : Format and display 6 numbers to the screen  
    public void ticket()
    {
        System.out.print("**         ");
        for (int i = 0; i < numberLine.length; i++) 
        {
            if (numberLine[i] < 10) 
            {
                System.out.print(" ");
            }
            System.out.print(numberLine[i] + " ");
        }
        System.out.print("        **\n");
    }
 }

The Ticket class will get the numberOfLines from the user at runtime, then print out the first half of the lotto ticket and then use the Numbers class to generate and print the random numbers, format them, then print them to the screen and finally print the bottom of the ticket.

What I should do really, is have the arrays from the Numbers class be stored into an arraylist in the Ticket class and then format the lines and print them from the Ticket Class.

Question 1: How would I go about adding the random number lines (the arrays) into an arraylist in the Ticket class?

Question 2: How would I format the lines if they are in an arraylist?

Upvotes: 3

Views: 8590

Answers (1)

unholysampler
unholysampler

Reputation: 17341

To make a list of arrays, you need to declare the list as holding an array.

List<int[]> arrs = new ArrayList<int[]>();

Once you have that, you will need to use two loops to display the sets of results. One to iterate over the arrays in the list and the one you have now to print the array of numbers.

Upvotes: 3

Related Questions