Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog
An array is 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 the array 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) in Studentlist.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
Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php
Swift 2.0 Learning Note (day)--swift array Collection