Hello,
It appears that I'm having a slight problem with a pesky ArrayIndexOutOfBoundsException when trying to assign a value to an array embedded within an object. I think it's because the array hasn't been instantiated before I try to add values to it's index.
The purpose of the program is to generate an array of random numbers within each object of the population.
So, in the following case there will be 10 objects each with their own 8 random number sequences.
Firstly, here is a code snippet from my main class.
int a = 10
int b = 8
Random random = new Random();
//create a population of objects
Object[] population = new Object[a];
//create temporary object
Object currentObject = new Object(b);
for (int i = 0; i < a; i++) {
//create a random sequence of numbers within temporary object
for (int j = 0; j < b; j++) {
int numberValue = random.nextInt(11);
currentObject.setValue(j, numberValue);
}
population[i] = currentObject;
}
And secondly, a code snippet from my object.
public class Object {
int numbValues;
int[] values = new int[numbValues];
Object(int numbValues) {
this.numbValues = numbValues;
}
public void setValue (int index, int value) {
values[index] = value;
}
}
Appreciate all help given, thanks in advance.