Exploring a possible solution
Now that we understand the limitations of using variables and how they prevent us from designing solutions at scale, we can look at the data structure designed to address these problems.
An array is a contiguous (placed one after another with no gaps) segment of memory that can store multiple data items simultaneously. In its simplest form, an array has a fixed size and can store only a fixed number of data items. All items in an array must be of the same type.
An array data structure.
Both restrictions exist for speed. When every item takes the same number of bytes and the items sit one after another, the computer can calculate exactly where any item lives and jump straight to it. The lesson on internal mechanics later in this module shows the exact calculation.
An array is given one contiguous block of memory when it is created, and the memory right after that block may already be in use by other data. The block therefore cannot grow in place. If more space is needed, a bigger block must be reserved and all the values copied over.
Let us revisit the problem of storing the ages of all students in a class. We could solve the problem by storing the age of each student in a separate variable, but it does not scale well when dealing with tens or hundreds of students. An array helps us solve this problem, as we can create a single array to store the ages of all students.
Storing the ages of students in a class in an array