Array Length and Looping
Knowing the length of an array is very useful, especially in the context of looping, or iterating.Length
You can find the length of an array using thelength
attribute. An attribute is different than a function. Whereas a function requires parameters and returns a value, an attribute is simply a variable that has been made public to you.
var animals = ["cat", "dog", "bird"];
print(animals.length);
Output:
3
Note that we used animals.length
, NOT animals.length()
.
Looping
When you know the length of an array, you can use a for loop to access each element. The following code prints each element of theanimals
array in order.
for (var i = 0; i < animals.length; i += 1) {
println(animals[i]);
}
Output:
cat
dog
bird