dimanche 7 mars 2021

Issue with assigning random values to an array

I have been trying to write a method that when called upon will produce an array of a random length with random numbers assigned to each value. For some reason, I am getting an out of bounds exception inside the for-loop.

   public static int[] randArray()
   {
       int[] myRandArray = new int[0];
       
       for (int i = 0; i <= (int)((Math.random() * 99) + 1); i++ ) {
           myRandArray[i] = (int)((Math.random() * 99) + 1);
       }
       
       return myRandArray;
   }

I'm very much so a newbie to Java and this is probably a dumb question but thanks anyways.




Aucun commentaire:

Enregistrer un commentaire