Get startedGet started for free

Introduction to arrays

1. Introduction to arrays

So far we have been working with individual pieces of data. To work efficiently with large datasets we need to learn about arrays.

2. What is an array?

An array is a data structure that holds an ordered collection of elements. Say you wanted to store all of your run times. Previously we assigned each run time to it's own variable. This becomes cumbersome when we have a lot of values. Instead, we can collect the data into a list of values. The list must be surrounded by square brackets and have commas between the values. This creates an array, and we can assign the array to a single variable name.

3. Arrays vs. vectors vs. matrices

If we print the type of the variable runtimes, we see that Julia says this is a vector rather than an array.

4. Arrays vs. vectors vs. matrices

In Julia, a vector is just a one dimensional array. As you learn more about Julia you will also come across matrices.

5. Arrays vs. vectors vs. matrices

A matrix is a two dimensional array.

6. Arrays vs. vectors vs. matrices

If an array has three or more dimensions it is just referred to as an array. In this course we will only be using vectors.

7. Array data types

When we print the data type of the array, Julia also prints the data type of the array elements inside curly brackets. In this case, Julia has recognized that the data are all floats.

8. Array data types

We can find the data type of the array elements alone by using the eltype function instead of the typeof function.

9. Array data types

It is possible to have an array of any data type. We can have an array of integers, an array of strings, an array of characters, or an array of booleans. Julia will recognize the data type of the elements when you construct an array and create an array of the matching type.

10. Mixed data types

We can also fill an array with a mixture of types. In this array, we have five different data types. In this case, Julia will create an array with data type 'Any'. However, creating arrays with data type 'Any' is bad practice. Our code will usually run faster and use less memory if we only use a single data type in an array.

11. Mixed data types

If we want to store multiple data types, it is better to create separate arrays for them. In this example, where we are storing item names and prices for a shop, we create one array of strings to hold the item names and another array of floats to hold the prices.

12. Indexing arrays

Once we have created the arrays, we can use indexing to extract individual elements from them. Like strings, we select elements from the array by following the variable name with an index number inside square brackets. The first element has an index of one; the second has an index of two, and so on.

13. Indexing arrays

We can also use the 'end' keyword to index the final element of the arrays,

14. Indexing arrays

and to index backward from the end.

15. Slicing arrays

Slicing works similarly to indexing. We select the start and end index and extract all values between these. Slicing returns a copy of this section of the array.

16. Let's practice!

Now let's practice in the exercises.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.