0

I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.

Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
    if (skateboard.Code != _skateboards[i].Code)
    {
        newSkateboard[i] = _skateboards[i];
    }
}
1
  • Use list<t> instead of arrays. Almost allways! Commented Oct 5, 2022 at 8:03

1 Answer 1

1

Sure.

    var j = 0;
    for (int i = 0; i < _skateboards.Length; i++)
    {
        if (skateboard.Code != _skateboards[i].Code)
        {
            newSkateboard[j] = _skateboards[i];
            j = j + 1;
        }
    }
Sign up to request clarification or add additional context in comments.

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.