Swift array


Swift arrays use ordered lists to store multiple values ​​of the same type. The same value can appear multiple times in different positions in an array.

Swift arrays will be forced to detect the type of elements. If the types are different, an error will be reported. Swift arrays should follow the form like Array<Element>, where Element is the only data type allowed to exist in this array.

If you create an array and assign it to a variable, the created collection can be modified. This means that after creating an array, you can change the items in the array by adding, deleting, or modifying. If you assign an array to a constant, the array cannot be changed, and neither the size nor the contents of the array can be modified.


Creating an array

We can use the construction syntax to create an empty array composed of a specific data type:

var someArray = [SomeType]()

The following is the syntax to create an initialized size array :

var someArray = [SomeType](count: NumbeOfElements, repeatedValue: InitialValue)

The following example creates an empty array of type Int, size 3, and initial value 0:

var someInts = [Int](count: 3, repeatedValue: 0)

The following example creates an array with three elements:

var someInts:[Int] = [10, 20, 30]

Accessing the array

We can access the elements of the array according to the index of the array. The syntax is as follows:

var someVar = someArray[index]

index The index starts from 0, and index 0 corresponds to the first one element, index 1 corresponds to the second element, and so on.

We can learn how to create, initialize, and access arrays through the following examples:

import Cocoa

var someInts = [Int](count: 3, repeatedValue: 10)

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

The output result of the execution of the above program is:

第一个元素的值 10
第二个元素的值 10
第三个元素的值 10

Modify the array

You can use the append() method or the assignment operator += to add elements to the end of the array. As shown below, we initialize an array and add elements to it:

import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

The output of the above program is: :

第一个元素的值 20
第二个元素的值 30
第三个元素的值 40

We can also modify the value of the array element through the index:

import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

// 修改最后一个元素
someInts[2] = 50

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

The output result of the execution of the above program is:

第一个元素的值 20
第二个元素的值 30
第三个元素的值 50

Traverse the array

We can use a for-in loop to traverse all data items in the array:

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("php")
someStrs += ["Google"]

for item in someStrs {
   print(item)
}

The output of the above program execution is:

Apple
Amazon
php
Google

If we need the value and index of each data item at the same time Value, you can use String's enumerate() method to perform array traversal. The example is as follows:

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("php")
someStrs += ["Google"]

for (index, item) in someStrs.enumerate() {
   print("在 index = \(index) 位置上的值为 \(item)")
}

The above program execution output is:

在 index = 0 位置上的值为 Apple
在 index = 1 位置上的值为 Amazon
在 index = 2 位置上的值为 php
在 index = 3 位置上的值为 Google

Merge arrays

We can use the addition operator (+) to merge two existing arrays of the same type. The data type of the new array will be inferred from the data types of the two arrays:

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)

var intsC = intsA + intsB

for item in intsC {
   print(item)
}

The output result of the above program execution is:

2
2
1
1
1

count attribute

us You can use the count attribute to calculate the number of array elements:

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)

var intsC = intsA + intsB

print("intsA 元素个数为 \(intsA.count)")
print("intsB 元素个数为 \(intsB.count)")
print("intsC 元素个数为 \(intsC.count)")

The execution output of the above program is:

intsA 元素个数为 2
intsB 元素个数为 3
intsC 元素个数为 5

isEmpty attribute

We can use the read-only attribute isEmpty To determine whether the array is empty, return a Boolean value:

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = [Int]()

print("intsA.isEmpty = \(intsA.isEmpty)")
print("intsB.isEmpty = \(intsB.isEmpty)")
print("intsC.isEmpty = \(intsC.isEmpty)")

The output result of the above program execution is:

intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true