Arrays
The following information should be considered additional information to that already provided by Microsoft as part of their MakeCode Blocks Arrays Reference.
You can find more information about arrays on the BBC Bitesize page about arrays and lists.
What is an array?
In the MakeCode platform, an array is a special type of data structure that can be used to store an ordered collection of data. Essentially, it is a list containing zero or more items of data. Yes, that's right, it's a list.
There are different terms that are used when talking about the items contained within an array. These words all mean the same thing. Common words are: item, entry, element.
What can be stored in an array?
Pretty much anything can be stored in an array. In some programming languages, each element of an array must be the same type but in MakeCode this is not the case; each element can be of a completely different type.
How many items can be stored in an array?
Arrays in the MakeCode platform are dynamic arrays. That means that the number of items
contained within an array can change over time as new items are added and others removed.
The number of items in an array is usually called the arrays length
. An empty array has a
length of zero whilst an array containing the numbers 17, 2, 31 and 55 has a length
of 4.
How to access elements in an array?
The elements in an array are typically accessed by their position within the array. This is
usually called the index
of the item. Arrays in most languages (including the MakeCode
platform) use zero as the index
for the first element. Yes that's right, zero. The second
element of an array uses the index 1
, the third element of the array uses the index 2
and
so on. This means that the first element of an array has the index 0
and the last element
has an index of array length - 1
. Remember, the number of elements in an array is
usually called it's length
.