In Swift Language Tutorial for Beginner 5 - Optional, we’ve seen the use of optional type in Swift, and now let’s take a look at collection types - Array(list) in Swift
Among Swift’s data types, there are three basic collection types, which are: 1. Array; 2. dictionary; 3. Set. All three data types are available in other languages, such as in Java: List, Map, and Set, and also in C++’s STL libraries.
Based on this, this article will not describe the characteristics of these types (e.g. elements in a Set cannot be duplicated), but will focus on how to use these types in Swift. Let’s start with lists - Arrays
Array Declaration
Declare an integer array
1 | var array1:[Int] |
Of course, we can also initialize at the same time as the declaration, such as:
1 | var array1:[Int] = [1, 2, 3] |
Array initialization
1 | var array1 = [] |
For cases where you need to assign a value repeatedly, you can also use:
1 | var array3 = [String](repeating: "Hello", count: 10) |
Or a number:
1 | var array4 = Array(repeating: 1, count: 10) |
In contrast to other languages, a loop statement specifically designed for initialization is omitted.
Determine how many elements are in Array
1 | array1.count |
Note: count is a property and not a method
Determine if Array is empty
1 | if array1.isEmpty { |
Note: IsEmpty is a property not a method
Merge the two arrays
To merge two arrays, you can add the two arrays directly with a “+” sign
1 | var array1 = [1,2,3]+[4,5,6] |
Gets an element in the array
Directly with subscript
1 | var a = array1[0] |
Of course, it is also possible to assign values, such as:
1 | array1[0] = 100 |
Slice
Takes a subset of the elements in an array, and the result is an array of the same type
1 | var array = [1,2,3,4,5,6,7,8,9] |
You can also assign a value directly to a subset, such as:
1 | var array = [1,2,3,4,5,6,7,8,9] |
Append one or more elements
1 | //Appends an element to the array |
1 | //Appends a set of elements to the array |
Inserts one or more elements into a specific location
1 | // Inserts an element to a location in the array |
1 | // Inserts a set of elements to a position in the array |
Deletes one or more elements at the specified location
1 | // Removes an element at a location in the array |
1 | // Removes the element at the first position in the array |
1 | // Removes the element in the last position |
1 | // Remove First Elements Parameter is the number of elements to remove |
1 | // Remove Last Elements Parameter is the number of elements to remove |
1 | // Removes an element in scope |
1 | // Remove all elements |
Replacement
1 | // Modify an element in scope |
Note: This substitution operation contains a delete operation at the same time, because there are only two alternate elements and three positions
Determine if an element exists
1 | if array.contains(1){ |
Max、Min
1 | // Gets the maximum value in the array |
1 | // Gets the minimum value in the array |
Iterates through array elements
You can iterate through array elements directly with the for statement
1 | for item in array { |
It can also be iterated using subscripts
1 | for index in array.indices{ |
Sorting
1 | // Sort from largest to smallest |
1 | // Sort from smallest to largest |
Next
Next, we’ll take a brief look at dictionary types in Swift – Swift Language Tutorial for Beginner 7 - Dictionary.