Now that we have familiarized ourselves with the different types of data, let’s discuss how to store and use it. Arrays are structures consisting of multiple elements, and they are useful for when we want to store related data. It can be hard to understand how an array works, so visualizing an array can make this a little easier. Here is what an array looks like:
Let’s start with the shaded figure that is divided into square boxes. This figure is an array. Each square box within this figure contains an element, or one piece of data, that we want to store in this array. As you can see, each element in this array is an integer. Hence, we would call this an array of integers. However, you can store any type of data in your array including ints, doubles, chars, etc. Now, let’s move on to the numbers that go from 0 through 5 that are listed above the elements of the array. The numbers 0 through 5 represent the index values of the array. In the array, there are 6 elements (represented with 6 square boxes). Each element has its own index value, listed above the element. The first element is always assigned an index value of 0 → that’s why the square box containing 2 has a 0 above it. From there, the second element is given an index value of 1, and so on. Knowing how the index values of an array work are vital to understanding how to store and retrieve elements from an array.
Next Section
7.2 Declaring ArraysCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.