Comment on page


Arrays are an ordered list of some type. In Swift, arrays are very similar to python or ArrayLists in java–they are entirely mutable in terms of both their contents and length.


Generally speaking, you will see the type for an array as their element type enclosed with square brackets [someType]. For example, a list of integers would have type [Int]. This works for nested lists as well. Say we have a 2-dimensional list of integers, an element in the top-level list would be a list of integers. Therefore, the type of the original list is [[Int]]. In fact, the type of any n-dimensional list of type m has n pairs of square brackets surrounding m.
The reason I preface the previous paragraph with "generally", is that we normally code in array literals. The common notation being [ element1, element2, ... ]. But arrays are actually a generic–exactly as you would see in java–and thus can be represented as such: [Int] is the same as Array<Int>. You will rarely see the array generic as it is convention, and is a lot cleaner, to use literals. So you don't really need to worry about this other form of arrays.


There are many different ways to create arrays depending on how you want them to be initialized. If you want them to be completely empty, use the following:
  1. 1.
    Use a type annotation to declare the type of the list and then write the empty list
  2. 2.
    Instantiate the type literal: var x = [Int]()
var x: [Int] = []
// x: [Int] = []
var y = [Int]()
// y: [Int] = []
These are functionally the same, so it comes down to your stylistic preference.
If you have data that you want to populate the list with this gets quite a bit easier because we can take advantage of inferred types. We simply write a typical array literal:
var z = [1, 2, 3]
// z: [Int] = [1, 2, 3]
There is one other way to initialize a list that is quite useful. While arrays are mutable, it may be useful to use a list immutably (in the sense of their length). For example, if you are making a queue or a hashmap, it may make sense to keep an array limited to a certain size. To do this, we need to start with an array of a certain size. This is an instance where we need to turn to the verbose array form. To do this, we use the Array(repeating: T, count: Int) initializer, where T is an instance of a type. This will create a list whose length is count, where every element in the list is set to be repeating. For those of you comfortable with generics, you may notice we never specify what T is. This is again because of inferred typing.
var emptyList = Array(repeating: 0, count: 10)
// emptyList: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The last way to instantiate an array is with a sequence. You can create array slices from arrays:
let myNumbers = [0, 1, 2, 3]
// myNumbers: [Int] = [0, 1, 2, 3]
let mySlice = myNumbers[1...2]
// mySlice: ArraySlice<Int> = [1, 2] <- this is a simplification
A slice is not an array (this has to do with protocols with associated types–an advanced topic). ArraySlice is something we call a Sequence. For this context, this means that if you take an array slice and want to convert it to an array, you can use Array(_:sequence). If that didn't make sense, you can come back after reading the lower sections. And if it still doesn't we don't use this very much it is okay. Just remember, if you get something of array slice, you may want to re-read this section.
var emptyList = Array(repeating: 0, count: 10)
// emptyList: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var mySlice = emptyList[1..<3]
// mySlice: ArraySlice<Int> = [0, 0]
var myNewArray = Array(mySlice)
// myNewArray: [Int] = [0, 0]
I am sure there are more ways to instantiate lists–these are just the ones I have found particularly useful and common.