I am trying to make a list program but i did something wrong
It gives an array index error, can you tell me what i did wrong?
14 Replies
the array should be dynamic idk why it does not work
at the very beginning u have
int i = list.Length
, thats already an invalid index
indices start with 0, so the range of valid indices is from 0 to (Length - 1)
also note that string[]
are arrays not lists, arrays are somewhat similar, but their size does not change
actual lists like List<string>
are more complex types, which allow adding and removing elementsso i cant use arrays?
or can i use new?
because the very first thing u ask the user is "how many", u still can, u just have to create the array using that number to set the length:
string[] list = new string[max];
oh
between the
[
and ]
goes the size u wantbtw thanks
glad i could help 🙂
@cap5lut what did i do wrong now+
?
well,
max
is the length of the array, right?
and max - 1
is the last valid index for that array
so if i
is max + 1
, its already 2 more than the last valid index
and u do that check after accessing the array, so it errors before u can actually do the check
because while
loops take already a condition u can basically modify it there
alternatively u could also write this as for
loop:
thanks again
idk what i was thinking
u probably thought of
i + 1 == max