9.26. Swift array

发布时间 : 2025-10-25 13:33:44 UTC      

Page Views: 10 views

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

The Swift array forcibly detects the type of the element, and if the type isdifferent, it will report an error. The Swift array should follow somethinglike Array in this form, of which Element is the only data type allowed in this array.

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

9.26.1. Create an array #

We can use construction syntax to create an empty array of specific data types:

var someArray = [SomeType]() 

The following is the syntax for creating an initialized size array:

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

The following example creates a type of Int an empty array with a quantity of 3 and an initial value of 0

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

The following example creates an array of three elements:

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

9.26.2. Access array #

We can access the elements of the array based on the index of the array. Thesyntax is as follows:

var someVar = someArray[index] 

The index index starts at 0, that is, index 0 corresponds to the first 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](repeating: 10, count: 3) var someVar = someInts[0] print( "The value of the first element \(someVar)" ) print( "The value of the first element \(someInts[1])" ) print( "The value of the first element \(someInts[2])" ) 

The output of the above program execution is as follows:

The value of the first element is 10 The value of the second element is 10 The value of the third element is 10 

9.26.3. Modify array #

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

import Cocoa var someInts = [Int]() someInts.append(20) someInts.append(30) someInts += [40] var someVar = someInts[0] print( "The value of the first element is \(someVar)" ) print( "The value of the second element is \(someInts[1])" ) print( "The value of the third element is \(someInts[2])" ) 

The output of the above program execution is as follows:

The value of the first element is 20 The value of the second element is 30 The value of the third element is 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] // Modify the last element someInts[2] = 50 var someVar = someInts[0] print( "The value of the first element is \(someVar)" ) print( "The value of the second element is \(someInts[1])" ) print( "The value of the third element is \(someInts[2])" ) 

The output of the above program execution is as follows:

The value of the first element is 20 The value of the second element is 30 The value of the third element is 50 

9.26.4. Ergodic array #

We can use it. for-in loop through the data items in all the arrays:

import Cocoa var someStrs = [String]() someStrs.append("Apple") someStrs.append("Amazon") someStrs.append("Runoob") someStrs += ["Google"] for item in someStrs { print(item) } 

The output of the above program execution is as follows:

Apple Amazon Runoob Google 

If we need both the value and the index value of each data item, we can use the String of enumerate() method to traverse the array. Examples are as follows:

import Cocoa var someStrs = [String]() someStrs.append("Apple") someStrs.append("Amazon") someStrs.append("Runoob") someStrs += ["Google"] for (index, item) in someStrs.enumerated() { print("The value at index=\ (index) position is \(item)") } 

The output of the above program execution is as follows:

The value at index=0 is Apple The value at index=1 is Amazon The value at index=2 is Runoob The value at index=3 is Google 

9.26.5. Merge array #

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

import Cocoa var intsA = [Int](repeating: 2, count:2) var intsB = [Int](repeating: 1, count:3) var intsC = intsA + intsB for item in intsC { print(item) } 

The output of the above program execution is as follows:

2 2 1 1 1 

9.26.6. count attribute #

We can use count property 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 elements number is \(intsA.count)") print("intsB elements number is \(intsB.count)") print("intsC elements number is \(intsC.count)") 

The output of the above program execution is as follows:

intsA elements number is 2 intsB elements number is 3 intsC elements number is 5 

9.26.7. isEmpty Attribute #

We can use the read-only property isEmpty to determine whether the array is empty and 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 of the above program execution is as follows:

intsA.isEmpty = false intsB.isEmpty = false intsC.isEmpty = true 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。