Arrays

Arrays

Language

The code in this article will be in C#, JavaScript and Java.

Resources

Arrays Simplified

Arrays are one of the most fundamental data structures to learn.

When I think about how to apply the idea of arrays to the real world and then translate that idea back to computers, I like to start by thinking about a box of chocolates. See below image.

assorted-chocolates.jpg

Some boxes of chocolates come with a key that tells you what flavor chocolate is in which spot and some boxes of chocolates don't come with a key, and so like Forrest Gump said "you never know what you're gonna get".

We're going to assume our box of chocolates came with a key, so we can learn this array concept.

Let's say at the top left corner, we have the Dark Caramel Patties. After that, we are going to move to the right until we get to the end of the top row. So, let's say in order the top row is the following:

  1. Dark Caramel Patties
  2. Milk Bordeaux™
  3. Milk Beverly®
  4. Milk Mayfair®
  5. Dark Marzipan
  6. Dark Normandie®
  7. Milk Almond Caramel

We'll just work with the top row to learn about arrays. However, if we were to continue to the next row we would continue the sequence starting with 8, and sequentially move from left to right, naming each chocolate, as we did with the first row.

If you want to find a chocolate by name, also known as searching, you would start with the first chocolate and read through the list, until you find the name of the chocolate you're looking for.

For example, if you are wanting to eat the Dark Marzipan and you don't know where it is off the top of your head, you would read the name of each chocolate, starting with the first one.

You will finally find the Dark Marzipan in the fifth spot.

From Chocolate to Computer

Let's start relating the chocolates to computers before we get too far ahead.

Each chocolate square is a block of memory in a computer.

Each square that is next to another square are contiguous squares, aka they are connected and this makes them an array. So, each row of contiguous squares is an array.

Each block of memory (chocolate square) in an array (box of chocolates), is numbered sequentially. This number is called an index.

For simplicity, let's stick with 1-7, but know that the numbers could be something like 1001, 1002, 1003, etc. These numbers, also known as an address, are for the computer to know and keep track of, not us.

In most programming languages, we begin counting at 0 for finding indexes.

So, in our chocolate example, we searched for the Dark Marzipan and found it at the 5th position, because we started counting at 1.

However, to translate this to a programming language such as JavaScript or C#, our array of chocolates indexes become the following:

  1. Dark Caramel Patties
  2. Milk Bordeaux™
  3. Milk Beverly®
  4. Milk Mayfair®
  5. Dark Marzipan
  6. Dark Normandie®
  7. Milk Almond Caramel

This means our Dark Marzipan has an index of 4, if we were to search for the chocolate by name.

Concepts you've learned so far:

  • block of memory
  • array
  • index
  • searching an array

There are three other actions you could take working with arrays. So, let's go back to our chocolates example to learn these actions.

Get Chocolate By Index

Remember I briefly mentioned that the memory address is for the computer to know and keep track of. This means, if you were to write code that looks for the index 3, the computer would instantly know that at the memory address 3 is the Milk Mayfair® chocolate.

When you give the computer an index number, it does not have to search through each memory block to find and tell you what lives there.

The computer only has to search through each memory block when you don't know the index number aka the memory address.

Eat Chocolate

When you eat a chocolate, this would be known as deleting or removing an item from the array.

When you remove an item from the array, you are left with an empty memory block, which breaks the array and how the array was working.

To fix this, you would shift each chocolate to the left to close the empty spot and keep the sequencing starting from 0 to some-number.

Add Chocolate

If you look at the image of chocolates, you would see that if you tried to add a new chocolate to the box, there isn't a space for it.

If you relate this to a computer, if you run out of memory on your computer, you wouldn't be able to save anything or download anything until you either:

  • Delete other items on your computer to make room for new items
  • Upgrade the memory on your computer to hold all of your current items (data) and new data

Space and Time Complexity

This is covered in the book resource I linked, however, I will cover Big O notation in other articles.

The goal with this article is to keep the concept of arrays, memory and how those things work as simple as possible, before throwing in Big O notation.

Implementations

Arrays in C

There are several different ways to implement arrays in C# that I linked in the Resources section, however, this is my preferred way:

int[] numbers = { 1, 2, 3, 4, 5 };

Arrays in JavaScript

let numbers = [];

Arrays in Java

int[] numbers = { 1, 2, 3, 4, 5 };

Next Steps

Want to learn about other data structures? Check out the Start Here page to see other topics in this series.