Sorry to bother again, but I have more problems =|
I have a main class where a method is called:
public static void main(String[] args)
{
int[] array1 = ArrayUtils.randomArray(10, 6) ;
}
And then there is the method being called:
Generates a random array
@param n the size of the array
@param range the random numbers go from 0 to range - 1
^ Instructions given by my teacher, below is what i tried
*/
public static int[] randomArray(int n, int range) {
Random arraygen = new Random(); // I guess I'd have to create a random variable...
int[] array2 = new int[n]; // So this statement 'should' create a random array everytime its called?? (with value 'n' taken when its called in the tester class)
int ranger = arraygen.nextInt(range-1);
for (int i=0;i<=array2.length;i++) // going thru each value of the array, length was 'n'
{
array2[i] = arraygen.nextInt(ranger); //Create random value for each array item???
// System.out.println("Random array value " + i + " is " + array1[i]); //Random values DO get printed if i uncomment this, but then program crashes afterwards
}
Even if I uncomment the 'system.out.println' line near the end, I do get all the random values but at the end it says:
java.lang.ArrayIndexOutOfBoundsException: 10
at ArrayUtils.randomArray(ArrayUtils.java:48)
at ArrayUtilsTester.main(ArrayUtilsTester.java:9)
Help?