Custom Size and Loops
One of the benefits of arrays is that they allow you to specify the number of their elements at run-time: the size declarator can be a variable, not just an integer literal. Hence, depending on run-time conditions such as user input, we can have enough space to store and process any number of values.
In order to access the elements of whose size is not known until
run-time, we will need to use a loop. If the size of myArray
comes
from user input, it wouldn’t be safe to try to access a specific element
like myArray[5]
, because we cannot guarantee that the array will have
at least 6 elements. Instead, we can write a loop that uses a counter
variable to access the array, and use the loop condition to ensure that
the variable does not exceed the size of the array.
Example
In the following example, we get the number of elements at run-time from the user, create an array with the appropriate size, and fill the array.
Observe that:
- If the user enters a negative value or a string that does not
correspond to an integer for the
size
value, our program will crash: we are not performing any user-input validation here, to keep our example compact. - The loop condition is
counter < size
because we do not want the loop to execute whencounter
is equal tosize
. The last valid index incustomArray
issize - 1
. - We are asking for the
{counter +1}th
value because we prefer not to confuse the user by asking for the “0th” value. Note that a more sophisticated program would replace “th” with “st”, “nd” and “rd” for the first three values.
The Length Property
Every single-dimensional array has a property called Length
that
returns the number of the elements in the array (or size of the array).
To process an array whose size is not fixed at compile-time, we can use this property to find out the number of elements in the array.
Example
Observe that this code does not need the variable size
.
Note: You cannot use the length property to change the size of the array, that is, entering
would return, at compile time,
When a field is marked as ‘read only,’ it means the attribute can only be initialized during the declaration or in the constructor of a class. We receive this error because the array attribute, ‘Length,’ can not be changed once the array is already declared. Resizing arrays will be discussed in the section: Changing the Size.
For Loops With Arrays
-
Previously, we learned that you can iterate over the elements of an array using a
while
loop. We can also process arrays usingfor
loops, and in many cases they are more concise than the equivalentwhile
loop. -
For example, consider this code that finds the average of all the elements in an array:
- This can also be written with a
for
loop:
-
In a
for
loop that iterates over an array, the counter variable is also used as the array index -
Since we did not need to use the counter variable outside the body of the loop, we can declare it in the loop header and limit its scope to the loop’s body
-
Using a
for
loop to access array elements makes it easy to process “the whole array” when the size of the array is user-provided:
- You can use the
Length
property of an array to write a loop condition, even if you did not store the size of the array in a variable. For example, this code does not need the variablenumGrades
:
- In general, as long as the loop condition is in the format
i < <arrayName>.Length
(or, equivalently,i <= <arrayName>.Length - 1
), the loop will access each element of the array.