Please forgive my question if the answer is obvious. I'm a beginner at Java programming.
I have defined a class of objects. Each object has a number of fields and a number of methods.
I want to use an array of these objects.
In this case, my class is called "BestAveRecord", and my array of instances of BestAveRecord will have 221 elements or members. But for simplicity in this question, let's assume that there are only 3 elements in my array.
Firstly, I declare the array with
BestAveRecord[] bestAveragesArray = new BestAveRecord[3];
then I need to initialise each instance of BestAveRecord in the array, before the defined methods in the class will work on the elements.
If I assign dummy or random values to each element of the array, in the following way, for example,
BestAveRecord record0 = new BestAveRecord;
BestAveRecord record1 = new BestAveRecord;
BestAveRecord record2 = new BestAveRecord;
bestAveragesArray[0] = record0;
bestAveragesArray[1] = record1;
bestAveragesArray[2] = record2;
then everything works fine in the way I would expect things to work.
But in effect, I don't have an array at all, because I may as well work with 3 individual instances. It will get messy with 221 elements in the array, or 1,000 elements.
If I work with the array in a loop, as follows ...
int arrayIndex;
for (arrayIndex=0; arrayIndex< 3; arrayIndex++ ) {
bestAveragesArray[arrayIndex].setupRecord(list of arguments applicable to the arrayIndex);
}
where "setupRecord" is a method defined in the BestAveRecord class, which assigns values to some of the fields in a typical BestAveRecord,
then at the end of the loop, each of the three elements in the array will contain the values assigned to the last element, bestAveragesArray[2].
Even if I insert an independent instance of BestAveRecord, in this case called "dummyBARec", it makes no difference.
int arrayIndex;
BestAveRecord dummyBARec = new BestAveRecord();
for (arrayIndex=0; arrayIndex< 3; arrayIndex++ ) {
dummyBARec .setupRecord(list of arguments applicable to the arrayIndex);
bestAveragesArray[arrayIndex] = dummyBARec ;
}
At the end of this loop, each of the three elements in the array will contain the values assigned to the last element, bestAveragesArray[2], even though unique values were assigned to each element in the program logic.
This is my problem.
Please help.
--- Update ---
After I posted my initial question, the website referred me to similar threads, all of which I read and studied as best I could.
In the post "all objects in array the same?" (June 17, 2011), the coder has declared the fields in his/her object to be static. I did not do that.
I think my problem has more to do with the creation of memory for my array. I studied the thread "Initializing an Array of Objects" (April 20, 2012), where the conclusion is given as
"The programmer should (separately) initialize every single array cell with the new command".
I tried to do that, with each individual element, and in a loop.
BestAveRecord bestAveragesArray[0] = new BestAveRecord();
gives the compiler error message "Type mismatch: cannot convert from BestAveRecord to BestAveRecord[]"
I am trying to solve my problem by myself, but I'd appreciate help, because I'm hoping that it's just a simple matter of Java syntax.