mardi 20 octobre 2015

Print random array on one line: Java

Working on building an array that can generate random integer values inside an array. It prints the random numbers but it prints it like this [10][2][5][7][6][2][4][7][2][10][0]--->(down the side)--------> and want it to print like this [10,7,5,9,4,3,4,7,2,3] (one line).

   public class ArrayLab
{
    //array instance variable
    private int[] array1 = new int[10];

    //array constructor
    public ArrayLab(int integer)
    {
        //class parameter = 10
        int[] array1 = new int[]{integer};

    }

    public void initialize()
    {
         //allow randomization of numbers inside the array field.

        System.out.println(Arrays.toString(array1)); 
        //prints [0,0,0,0,0,0,0,0,0,0] which is ok

        System.out.println();
        for (int iteration = 0; iteration < array1.length; iteration ++)
        {
            Random number = new Random(); 
            number.nextInt(10);
            int n = number.nextInt(11);
            int[] array1 = new int[]{n};

            System.out.println(Arrays.toString(array1));
           //prints down the side. Want on one line?
        }

    }
}




Aucun commentaire:

Enregistrer un commentaire