Swift array

Swift arrays use ordered lists to store multiple values of the same type. The same value can appear more than once in different places in an array.

Swift arrays force detection of the type of element, which can be reported if they are different types, and Swift arrays should follow forms such as Array<Element;gt;

If you create an array and assign a value to a variable, the created collection is modifiable. T his means that after you create an array, you can change the items in the array by adding, deleting, and modifying them. If you assign an array to a constant, the array cannot be changed, and neither the size or content of the array can be modified.


Create an array

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

var someArray = [SomeType]()

Here's the syntax for creating an array of initialized sizes:

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

The following instance creates an empty array of types of Int, size 3, with an initial value of 0:

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

The following example creates an array of three elements:

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

Access the array

We can access the elements of the array based on the index of the array, as follows:

var someVar = someArray[index]

The index index starts at 0, and 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 with 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 of the above program execution is:

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

Modify the array

You can add an element at the end of an array using the append() method or the assignment operator, and as follows, 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 execution is:

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

We can also modify the values of array elements by indexing:

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 of the above program execution is:

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

Traverse the array

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

import Cocoa

var someStrs = [String]()

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

for item in someStrs {
   print(item)
}

The output of the above program execution is:

Apple
Amazon
W3CSchool
Google

If we need both the value of each data item and the index value, we can use String's enumerate() method for array traversal. Here's an example:

import Cocoa

var someStrs = [String]()

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

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

The output of the above program execution is:

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

Merge the array

We can combine two arrays of the same type that already exist using the addition operator . The data types of the new arrays are 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 of the above program execution is:

2
2
1
1
1

Count property

We can use the 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 元素个数为 \(intsA.count)")
print("intsB 元素个数为 \(intsB.count)")
print("intsC 元素个数为 \(intsC.count)")

The output of the above program execution is:

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

IsEmpty property

We can tell if the array is empty by the read-only property isEmpty, returning the 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:

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