DEV Community

Discussion on: how to fix Index was outside the bounds of the array error?

Collapse
 
waelelsayegh1 profile image
Wael El-Sayegh

to check the neighbors of the element in the array

Collapse
 
peledzohar profile image
Zohar Peled • Edited

this line states[i] = 0; doesn't make sense inside this condition, because you already know that the value in states[i] is 0.

And as for the exception - if you know i is smaller than the array's length, it means that i+1 must be equal to the array's length to throw that exception - so, guessing that this i is a for loop variable, you need the loop to stop one step before - so for(var i = 0; i < arr.Length - 1; i++) {/* do stuff here */}