Basic Array Operations

Count

The length of your array is accessible through the count field. To access it, just do myArray.count.

Get

Getting a single element is standard–it's your array followed by square brackets that contain the index of the element you want. Of course, make sure it is within the bounds of the array or else you will have a runtime error. Lists are 0-indexed meaning that the bounds for any list is 0 to one less than the length of the list.

let myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]

myList[0]
"a": String

myList[myList.count - 1]
"c": String

myList[-1]
// Fatal error: Index out of range
myList[myList.count]
// Fatal error: Index out of range

If you want to get a section of the array, or an ArraySlice, you need to use ranges. See the ranges page for their construction.

This range must correspond to indices that will be used in the array slice.

The section you specify will be used to create a new ArraySlice, which contains the information with limited functionality of arrays. To get this functionality back use the the Array(_: Sequence) initializer to convert back to an array.

let myList = ["a", "b", "c", "d", "e"]
// myList: [String] = ["a", "b", "c", "d", "e"]

let mySlice = myList[3..<myList.count]
// mySlice: ArraySlice<String> represents ["d", "e"]

let myNewList = Array(mySlice)

There are two more ways to get elements from an array, which are more advanced because they require an understanding of anonymous functions and optionals.

Put

We use subscript assignments to assign elements to pre-existing locations.

var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList[0] = "A"
// myList: [String] = ["A", "b", "c"]

To add a new element at the end of a list, or to add a new element when order does not matter, use the append(_: element) method.

var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList.append("A")
// myList: [String] = ["a", "b", "c", "A"]

To add an element at a specific location, we use the insert(_: element, at: Int) method. This method will shift every element after this location up by one, and then insert the element at the location:

var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList.insert("A", at: 1)
// myList: [String] = ["a", "A", "b", "c"]

Removal

To remove an element at a specific location use remove(at: Int).

var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList.remove(at: 1)
// myList: [String] = ["a", "c"]

Addition

What do you do if you have two lists that you want to concatenate? One technique would be sequentially appending them–iterate through one list and sequentially add the elements of the list to the other list. Another, more efficient and clean way is just to use the + operator.

let lst1 = ["a", "b", "c"]
// lst1: [String] = ["a", "b", "c"]
let lst2 = ["d", "e", "f"]
// lst2: [String] = ["d", "e", "f"]
let lst3 = lst1 + lst2
// lst3: [String] = ["a", "b", "c", "d", "e", "f"]

Last updated