Find first empty slot in an array of objects

18,435

You are doing just fine. Your only fault is that you are using the assigne operator = (single equals) in your if-condition.

Where you should use the comperator: == (double equals)

int openArray = 0;

for(int i = 0; i<markbook.length; i++) {
    if(markbook[i] == null)
    {
        openArray = i;
        break;
    }
}

And you should save i instead of 1. See my code example.

Share:
18,435
Jordan.McBride
Author by

Jordan.McBride

I try to code and use photoshop CC.

Updated on June 06, 2022

Comments

  • Jordan.McBride
    Jordan.McBride almost 2 years

    This is for a school project where we have to create an object, and then create an array of 20 objects. The object contains 1 string, and 4 doubles. I understand how to use a constructer to initialize the objects vairables. However, the part I am stumped on is how to determine the first empty space in the array. I'm assuming that each object in the array is null until it is assigned variables via the constructer. How would I go about finding the first empty spot in the array?

    Forgive me if its a duplicate, but the ones I looked at either didn't have thorough questions or they were not what I think I'm looking for

    I tried to do this:

    int openArray;
    
    for(int i = 0; i<markbook.length; i++) {
        if(markbook[i] = null)
        {
            openArray = 1;
        }
    }
    

    But it didn't seem to do anything, or work.

    -Jordan