You probably know that freight trains are an important means for moving cargo. But have you ever thought about the system that keeps things running smoothly as changes are made at every stop? Carriages are typically set in a specific sequence and each carriage may carry different items or materials. Other carriages can also be added or removed without disrupting the rest of the order. Much like a freight train, Swift has a piece of functionality that allows you to store and rearrange sequenced collections of items. This is called an array. In this video, you will be introduced to the concept of the array collection and how it is used to store data. You will also learn how to edit an array, how to use methods and perform operations such as append, insert and remove items in arrays. Let's say that you want to emulate a toy freight train using Swift. On this train, each carriage has a number painted on its side and is presented in sequence, starting from zero. Using Swift, you'll assign some cargo value to each carriage and store it in a variable. First, you declare a variable named carriage0 and then assign a string value of wheat. Great. Now, when you print the variable carriage0, the word wheat is output. You can use the same approach to model your entire train. Let's say you do just that and end up with five variables of different values. For example var carriage1 equal barley, var carriage2 equal salt, and so on. Wonderful. You've just built a train with five carriages, each holding different cargo. Now, let's make things interesting and introduce a second train. This one only has two carriages with the numbers 0 and 1 painted on the sides. Seems pretty simple, right? Well, now you can't use the variable names carriage0 and carriage1 because they're already in use by your first train. You could try and use more descriptive names such as train2carriage0 and train2carriage1, but now you are complicating your code. Another problem is that you need to signal that these variables are a collection belonging to train 2. But when written this way, Swift doesn't understand that. Luckily, you can fix this. To make Swift understand that you want to group a sequence of variables in a collection, you use arrays. Arrays store ordered lists of value of the same type. Swift arrays are specific about the kinds of values they can store. And you can do this by specifying the type or by allowing Swift to do it by type inference. For example, you can create an array of integers, floats, or strings, but you cannot mix them. You can build an array using the array literal syntax, which is written as a list of comma separated values surrounded by square brackets. To store an array, you assign that collection to a variable. For example, var train1 followed by a colon and the type of the value string within square brackets. Notice that in this case the data type of string is declared. Then you can add items to the array by typing the item names, making sure to separate each by a comma, and enclosing everything within a pair of square brackets. In this case, each item is in double quotes because they are string types. Once again, you can also let Swift determine what type the array is using type inference. So you do not have to declare the type of array to be stored. So you can just write var train1 equal without specifying the type of the item in the array. Okay, so now you know what an array is and how to create an array. Let's now explore how you can access values in an array. Do you remember how you built a train with carriage numbers? Well in Swift, this numbering system happens behind the scenes. This is known as the array index and you can then use these index numbers to access each array item. Swift gives each array item a number starting from zero that corresponds to its place in the sequence. Remember that in the example you had var train1 equal wheat, barley, potato, salt, rock. So wheat is index 0, barley index 1, potato index 2, and so on. To output the array item located at index zero, you would create a print function and inside the parenthesis type train1 followed by 0, encased in square brackets. This will access the first value of the array named train1 which is wheat. Okay, so now you know how to access array values using the index. Let's now find out how you can add, edit and delete items from an array. Let's start with editing an array value. And to do that you use the assignment operator. To edit the array item, you specify the item index value and then use the assignment operator to change its value. For example, if you have an array with two items wheat and barley and you want to change the value of the second item, you can have train1 and the index number 1 within square brackets then equal and sugar within double quotes. Once again it's important to remember that the value you change it to must be the same data type. Now to add and delete array items, you need to use something called array methods. They are like some of the methods that you have learned earlier. For example, do you recall using the count method to count the number of characters in a string? It was something like print, name.count. Well, you can use the same count method on an array to return the number of items in the array. For example, you can declare an array var train1 equal wheat, barley. Then you can use a print function, print, train1.count. This statement will return two because there are two items in the array. You also have specific array methods such as append, insert and remove. Let's explore them briefly now. You can use the append method to add an item to the end of the array. For example, train1.append salt, add salt to the array and if you want to add another item to the array, you type train1.append, rocks. Now your array has four items. You can also add an item to an array using the insert method, but you must specify the index and the value of where you want to insert it. For example, you can add an item by typing train1.insert potato, at:3. This will insert the item potato after the third item in the array. Finally, to remove an item, you can use the remove method. Here, you just need to specify the index of the item you want to remove. For example, if you want to remove the fourth item, you type train1.remove at:4. To summarize, arrays help us achieve several things in Swift. They signal that the assigned values all belong to a group. These values are ordered in a sequence and must be of the same data type. Each array item can be accessed through its index number. And you can use array methods to add and delete items from an array. In this video, you discovered how arrays are used to store multiple values of the same data type in an ordered manner using index values. You also learned that you can store any type of element in arrays like integers, floats, and strings. Arrays carry values like a freight train carries goods. They store and collect information that you can use in various ways.