Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to use an array of objects

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use an array of objects

    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.

  2. #2
    Junior Member
    Join Date
    Aug 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use an array of objects

    I have found my problem. As expected, it was a simple syntax error.

    I needed to initialise my array in a loop.

    My first attempt was

    BestAveRecord[] bestAveragesArray = new BestAveRecord[3];
    for (counter = 0; counter < 3; counter++ ) {
    BestAveRecord bestAveragesArray[counter] = new BestAveRecord();
    }

    which produced a compiler error : "type mismatch, BestAveRecord[] and BestAveRecord .

    I corrected my code to

    BestAveRecord[] bestAveragesArray = new BestAveRecord[3];
    for (counter = 0; counter < 3; counter++ ) {
    bestAveragesArray[counter] = new BestAveRecord();
    }

    and now everything works fine.

    This thread can now be deleted. Thank you to all who cared.

Similar Threads

  1. Objects in an Array/Classes Help
    By penguin0 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 29th, 2013, 10:45 PM
  2. Array and Objects trouble
    By incxx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2013, 10:55 PM
  3. Array of objects
    By spark in forum Object Oriented Programming
    Replies: 2
    Last Post: August 1st, 2012, 03:06 PM
  4. Initializing an Array of Objects
    By Destro in forum Object Oriented Programming
    Replies: 1
    Last Post: April 20th, 2012, 06:25 AM
  5. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 11:21 AM