Get startedGet started for free

Working with arrays

1. Working with arrays

Now we can create arrays by predefining them in a script.

2. Adding an element to the end of an array

But what if we want to modify the array after we have defined it? Here we have an array of integers, which we defined by typing them all out. We can add another element to this array using the push function. The push function takes the array x and extends it to add the extra element of five onto the end. The exclamation mark in the push function's name is Julia's standard style. A function that modifies its argument should end with an exclamation mark. When we print x, we can see that push has extended it.

3. Adding an element to the end of an array

The array x is an array of integers. If we try to add the value of 5-point-zero to the end of it, Julia will convert this value to an integer before adding it.

4. Adding an element to the end of an array

If the element we try to add cannot be converted to the data type of the array, then it will cause an error. Here the value 5-point-two cannot be converted to an integer exactly.

5. Creating an array of given type

When we create an array, we can choose the data type for it by writing the type before the square brackets. Here, the integers listed are converted to floats in the array, and now we can add the value of five-point-two.

6. Creating an array of given type

Using this data type annotation means we can define the data type of an empty array before we add any elements to it. Here we create an empty float array.

7. Creating an array of given type

We can use this format to create arrays of any data type available, like this array of strings.

8. Adding elements to the end of an array

Let's say we have a sequence of values we would like to add to an array. Instead of adding them one by one with push, we can add them all with the append function. The first argument to this function is the array to modify; the second argument is the array of values to add. The append function also has an exclamation mark because it modifies its inputs.

9. Removing the last element

We can also remove elements from the array. One method to remove elements is using slicing. Here we select all the elements of x except the last one. Another method is by using the pop function. This function removes the last element from the array, modifying it, and returns that element.

10. Creating array of defined length

Sometimes we will know how long our array of answers will be and simply need to calculate the values. When this is true, it is faster to create an array of the correct length and fill it with values, than to create an empty array and push values into it. We can create an array of zeros using the zeros function. The first argument is the data type, and the second is the array's length.

11. Replacing an element

Once we have created the array, we can fill it with values. We use square brackets to index the location in the array, and replace it by assigning a value. In this case, we select the third index, and set the value in this location to one.

12. Replacing many elements

We can use slicing to set multiple values at once. Here we select the indexes from two to three, and set the values in these locations to two and three.

13. Cheatsheet

We've covered a lot of syntax in this video. Here is a cheat sheet you can refer back to in the exercises.

14. Let's practice!

Let's practice now.