1

I'm working on a class project and here is what I have so far. I know the code returns true when it finds a match but I want it to keep looping until no more instances are found.

I've looked at numerous sites on for/while loops but I can't seem to get the syntax correct and/or it doesn't work when applying the logic.

public bool Remove(T toRemove)
{
    for (int i = 0; i < count; i++)
    {
        if (items[i].Equals(toRemove))
        {
            int removeIndex = i;
            for (int j = removeIndex; j < count - 1; j++)
            {
                items[j] = items[j + 1];
            }
            return true;
        }
    }
    return false;
}
2
  • 2
    Use a boolean variable and set it to true if an item is found, and continue your loop until you go through all elements, then return the boolean value. Commented Nov 9, 2018 at 15:30
  • 1
    That's awesome, didn't think of that. That did it, many thanks. Commented Nov 9, 2018 at 15:41

4 Answers 4

2

If you want to complete the loop, do not return. Instead hold the result on a var you should return at the end:

    public bool Remove(T toRemove)
    {
        bool result = false;
        for (int i = 0; i < count; i++)
        {
            if (items[i].Equals(toRemove))
            {
                int removeIndex = i;
                for (int j = removeIndex; j < count - 1; j++)
                {
                    items[j] = items[j + 1];
                }
                result = true;
            }
        }
        return result;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Just save the result in a variable and return it after the loop is complete:

public bool Remove(T toRemove)
{
    bool result = false;
    for (int i = 0; i < count; i++)
    {
        if (items[i].Equals(toRemove))
        {
            int removeIndex = i;
            for (int j = removeIndex; j < count - 1; j++)
            {
                items[j] = items[j + 1];
            }
            result = true;
        }
    }
    return result;
}

2 Comments

I think you meant bool instead of boolean.
@TânNguyễn true 'dat. Momentary lapse of concentration, thanks for noticing.
1
//Use a boolean variable and set it to true if an item is found, 
//and continue your loop until you go through all elements, then return the boolean value.  

public bool Remove(T toRemove)
{
        bool match= false; //boolean to track if any match is found
        for (int i = 0; i < count; i++)
        {
            if (items[i].Equals(toRemove))
            {
                int removeIndex = i;
                for (int j = removeIndex; j < count - 1; j++)
                {
                    items[j] = items[j + 1];
                }
                match= true;
            }
        }

        return match;
}

Comments

0

I think what you want to do is declare a bool called "result" and instantiate it to false. In the loop where you are returning true, set "result" to true. At the end, where you are returning false, return "result"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.