Swift Language Tutorial for Beginner 6 - Array

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
2
var array1:[Int]
var array2:Array<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
2
3
4
var array1 = []
var array2 = Array()
var array3 = [1,2,3]
var array4 = Array(arrayLiteral: 1,2,3)

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
2
3
if array1.isEmpty {
print("array1 为空数组")
}

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
2
var array = [1,2,3,4,5,6,7,8,9]
var subArray = array[0...3]

You can also assign a value directly to a subset, such as:

1
2
var array = [1,2,3,4,5,6,7,8,9]
array[0...3] = [11,22,33,44]

Append one or more elements

1
2
//Appends an element to the array
array.append(10)
1
2
//Appends a set of elements to the array
array.append(contentsOf: [11,12,13])

Inserts one or more elements into a specific location

1
2
// Inserts an element to a location in the array
array.insert(0, at: 0)
1
2
// Inserts a set of elements to a position in the array
array.insert(contentsOf: [-2,-1], at: 0)

Deletes one or more elements at the specified location

1
2
// Removes an element at a location in the array
array.remove(at: 1)
1
2
// Removes the element at the first position in the array
array.removeFirst()
1
2
// Removes the element in the last position
array.removeLast()
1
2
// Remove First Elements Parameter is the number of elements to remove
array.removeFirst(2)
1
2
// Remove Last Elements Parameter is the number of elements to remove
array.removeLast(2)
1
2
// Removes an element in scope
array.removeSubrange(0...2)
1
2
// Remove all elements
array.removeAll()

Replacement

1
2
// Modify an element in scope
array.replaceSubrange(0...2, with: [0,1])

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
2
3
if array.contains(1){
print(true)
}

Max、Min

1
2
// Gets the maximum value in the array
arraySort.max()
1
2
// Gets the minimum value in the array
arraySort.min()

Iterates through array elements

You can iterate through array elements directly with the for statement

1
2
3
for item in array {
print(item)
}

It can also be iterated using subscripts

1
2
3
for index in array.indices{
print(array[index])
}

Sorting

1
2
// Sort from largest to smallest
let arraySort = array.sorted(by: >)
1
2
// Sort from smallest to largest
let arraySort = array.sorted(by: <)

Next

Next, we’ll take a brief look at dictionary types in Swift – Swift Language Tutorial for Beginner 7 - Dictionary.

本文标题:Swift Language Tutorial for Beginner 6 - Array

文章作者:Morning Star

发布时间:2022年01月14日 - 09:01

最后更新:2022年01月17日 - 07:01

原始链接:https://www.mls-tech.info/app/swift/swift-tutorial-6-array-en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。