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.
new int[SIZE]. If you don't want to specify it via a passed value, then made use of the rng inside of the array initialization (to create something bigger than 0), then usemyRandArray.lengthlike usual.