An arrayis a series of ordered sets of elements of the same type. The collection elements in the array are ordered and can recur.
You can use one of the following statements when declaring an array type.
var studentlist1:array<string>
var studentList2: [String]
The declared array is not yet available and needs to be initialized, and thearray type is often initialized at the same time as the declaration. The sample code is as follows:
var studentlist1:array<string> = ["Zhang San ", "John Doe ", "Harry ", "Dong Liu "]
var studentlist2:[string] = ["Zhang San ", "John Doe ", "Harry ", "Dong Liu "]
Let studentlist3:[string] = ["Zhang San ", "John Doe ", "Harry ", "Dong Liu "]
var studentList4 = [String] ()
Array traversal
The most common operation of an array is traversal, which is to take each element of the array out and manipulate or calculate it. The entire traversal process is inseparable from the loop, and the for-in loop can be used .
The following is a sample code that iterates through an array:
var studentlist:[string] = ["Zhang San ", "John Doe ", "Harry "]
For item in Studentlist {
Print (item)
}
For (index, value) Instudentlist.enumerate () {
Print ("Item \ (index + 1): \ (value)")
}
The results of the operation are as follows:
Tom
John doe
Harry
Item 1: Zhang San
Item 2: John Doe
Item 3: Harry
Collection of arrays in Swift-B