# Arrays

Arrays are a fundamental data structure in programming. Since they're sometimes called a list, the best real-life analogy for an array would be a shopping list. Arrays let you store data in an ordered fashion. And by ordered, I mean that the data is stored in a numbered list the same way you would number your shopping list:

  1. Eggs
  2. Milk
  3. Cereal

And the way this would look in code is like this:

Copy code
      
    

So you'll notice that we start and end an array with [ and ]. This is how the computer knows you want the data to store in an ordered array. And then we define the strings "eggs", "milk" and "cereal" the same way we would define a string. And between each element in the array, we use , to tell the computer that we have another element we want to store in the array. And the ordering is simply defined by the order you specify the elements. So if we want to change the order to look like this:

  1. Cereal
  2. Eggs
  3. Milk

The code would be changed to look like this:

Copy code
      
    

So the next important part of arrays is how to access the data in an array. The data is only useful if there is a way to access it. To access data in an array, you'll need to specify the index (or position) in the array. This will then tell the computer to get the data at the index/position you specified. And here is what it looks like:

Copy code
      
    

So you'll see that we use the name of variable tempArray and then use [ and ]. And within the brackets we specify the index. You might be wondering why the "first" index is specified by 0 and not 1. So arrays always start with an index of 0 and if you specify 1 you're actually getting the second element in the array. And once you access the data at the index you want, you can use the data just like you would if it was a normal variable and not an array.

Next, we'll look at how to update an array. The simplest case would be something like changing "cereal" to "donuts". This will follow the last section on accessing an element at a specific index. And once you specify the element, you just use the = operator to set a new value. All together, it would look like:

Copy code
      
    

You now know how to create, read and update an array. The next thing we'll look at is combining what you learned about loops and how it applies to arrays. Think about a theoretical use-case where you have an array of items a user wants to purchase. You now need to write a program that reserves the items so they don't get sold to someone else. But you don't know how many items are in the list because one user might have 5 items and another user might have 50 items. Let's take a look at how to do this:

Copy code
      
    

Hopefully, the code makes sense, but we'll go through it and connect the dots. First thing to notice is when we defined the for loop, we specified tempArray.length which in our example would translate into 3. This allows us to make sure we go over each element in the array regardless of how many items are in it. The next thing you'll notice is that when we call the reserveItem we specify tempArray[ind]. This takes the variable ind which gets incremented on each loop and gets the element at that index. This gets passed into the function to make sure we're reserving every item in the list. And this is a fairly common pattern of using a loop to go through an array.

Next: Hashmaps