Explore everything about arrays in this video and why you would use them. Learn about how we can organize a collection of elements in an array with indices.
- Let's jump into the first data structure, our first container that holds data, an array. An array is like a row of seats at a movie theater. If you think about a row of seats, they're numbered depending on where they are located in the row. Zero, one, two, three, and so forth, and yes, with arrays, we're starting counting at zero. In the theater, we consider this row to be a collection of seats, and each seat has two options, a person sitting in it or it's empty.
In other words, each seat can have data in it or no data at all, but the seat is still numbered. You can think of this number as a label we can use to identify it later. Keeping this in mind, let's look at the definition of an array. Now formally, an array is a collection of elements where each item is identified by an index or a key, but let's break this down. Starting with the first part of the definition, an array consists of a collection of elements.
Here is a collection of numbers. In this case, we say they're ints or integers. There are six numbers in this collection. We also have a collection of characters. Now what would make each of these collections grouped into a data structure? Well, a data structure is a collection with a defined way to access and store items. This means we need to define a way to access each item in these collections. To make this an array, we need an index that identifies each element.
Looking at our second collection starting at zero, each character in the array will be associated with a unique numerical index, based on its position in the array. In this case, we can say the first character in the array, H, is at index zero. The second character in the array, E, is at index one. The third character, L, has index two and so forth. I find it useful to think of each index as a slot. Thinking in this way, we could say the index three slot has the value, L, and the index four slot has the value O.
If this still seems confusing, let's take a look at the movie analogy again. Imagine a single row of seats in a movie theater. Nowadays, each seat is associated with a specific index or number that identifies that seat. You have seat zero, one, two, et cetera. Think of our characters as the values in those seats, the values at those indices. The collection of the values in those seats make up the row and in essence, our array.
Now this index, no matter the data in your array, is very important. In programming, you'll often use this index to access a specific piece of data. For example, in Java, we can create an array with lottery numbers and use square brackets with an int index as input to get a specific integer in the array. In Python, we can create an array with the same contents and access the second number in the array with index one. The syntax differs depending on the language, but the main idea is the same.
It's also very important to remember what indices are valid for a given array. For our array of numbers, the only indices that are value are zero through five. If we try to access the element at index six or any other index, we will get an error, or more specifically, an array index out of bounds error. You will get this error because the program is trying to access an item at a position that is outside the array's limit, hence, out of bounds. Understanding how these indices work is essential to understanding arrays.
- Data types: Booleans, numbers, strings, and more
- Multidimensional arrays
- Jagged arrays
- Search and sort arrays
- Linked lists
- Stacks and queues
- Hash functions and hash tables
- Trees and graphs