Lists: Arrays
Lists: Arrays
If you've ever seen a stable full of horses—or cows or sheep or Komodo dragons—you already know the basic principles of arrays. Not in the sense that you should know how to ride a horse, cow, or Komodo dragon (though kudos to you if you can), but in the way that every beast of burden gets its own stall and every stall either has an animal or nothing in it.
Just don't try mixing the sheep with the Komodo dragons. You'll end up with no sheep and some very fat lizards.
It gets better, though. When you walk into a stable, you'll usually see numbers that each match up with one single horse. Sometimes you'll see an extra stall or two just in case another horse wants to move in. In the same way, every piece of data stored in an array is labeled with a number. Those pieces of data—called elements—are the data equivalent of an oversized lizard. An array can have as many spaces as there are elements, but it can also have some empty spaces in case you need to add more stuff in.
Just don't mix the types you throw in. You don't want to house your Komodo dragons with any potential Komodo dragon food, and you also don't want mix element types in an array.
No matter the programming language you're working in, you can always access the elements by their index number, usually like this:
arrayName[index]
array_name
stands in for…the name of the array, and index
is the…index number. To add or remove horses—erm, elements—just assign a different variable to the array_name
at index
.
Computer scientists' second favorite number after 1 is 0, so array indexing almost always starts at 0. If you're still picturing a stall of oversized lizards with terrible breath, the first Komodo dragon's stall would be labeled 0. That means that the number of elements will always be one greater than the largest index.
Assuming the array's completely full (it doesn't have any empty slots), the size of can be found by adding one to the last index (because…the 0th element). Want to find the reverse? Just subtract one from the size to find the last index. An array with a size of 2 has a last element of index 1 and an array whose last index is 4 has a size of 5.
When creating an array—or a stable, for that matter—you need to have some idea of how many spaces you'll need from the get-go. Arrays store their values all in the same place, one after another in computer memory. It's all in a continuous or uninterrupted block, but what comes before or after that block could be any piece of memory. It's usually difficult (if not impossible) to change the size of your array once you create it. Because of that fact, you need to tell the computer how much space it should allocate (set aside) for your array.
If, for example, you want to store an array of five integers (with indices 0 – 4), the computer's going to set set up something like this in memory.
Address | Value |
0x100 | 0 |
0x101 | 1 |
0x102 | 2 |
0x103 | 3 |
0x104 | 4 |
Once the computer has the space, you can call any value in the array using its index without having to worry about addresses or anything like that.
array_name[0] | array_name[1] | array_name[2] | array_name[3] | array_name[4] |
0 | 1 | 2 | 3 | 4 |
Because array sizes are immutable (unchangeable) 99% of the time, you'll want to give yourself some wiggle room. If you don't know exactly how much space you'll need, overestimate a little just in case. Even so, you don't want give all your arrays five billion spaces or anything—especially if you only need to store ten elements. That's just going to slow your computer down for no reason and make you run out of memory. Fast.
If you need a way to store an unknown amount of elements, you might need to go with a different data structure.
If you want to store horses of Komodo dragons, you're better off giving up your programming day job and buying a zoo.