Swift Learning Day Sixth: arrays

Source: Internet
Author: User

An introduction to array arrays
    • An array is an ordered set of elements of the same type
    • The collection elements in the array are ordered and can recur
    • Arrays in Swift
      • The swift array type is an array and is a generic collection
Initialization of the array
    • Array partitioning: mutable arrays and immutable groups
      • Arrays that use let adornments are non-variable groups
      • Arrays that are decorated with VAR are mutable arrays
// 定义一个可变数组,必须初始化才能使用var array1 : [String] = [String]()// 定义一个不可变数组let array2 : [NSObject] = ["why", 18]
    • You can use one of the following statements when declaring an array type
var stuArray1:Array<String>var stuArray2: [String]
    • The declared array needs to be initialized to be used, and the array type is often initialized at the same time as the declaration.
// 定义时直接初始化var array = ["why", "lnj", "lmj"]// 先定义,后初始化var array : Array<String>array = ["why", "lnj", "lmj"]
Basic operation of an array
// 添加数据array.append("yz")// 删除元素array.removeFirst()// 修改元素array[0] = "why"// 取值array[1]
Traversal of an array
// 遍历数组for i in 0..<array.count { print(array[i])}// forin方式for item in array { print(item)}// 设置遍历的区间for item in array[0..<2] { print(item)}
Merging of arrays
// 数组合并// 注意:只有相同类型的数组才能合并var array = ["why", "lmj","lnj"]var array1 = ["yz", "wsz"]var array2 = array + array1;// 不建议一个数组中存放多种类型的数据var array3 = [2, 3, "why"]var array4 = ["yz", 23]array3 + array4


Import UIKit//1. Definition of Arrays//Immutable Group (let) and variable array (VAR)//1> Non-variable groupLet array = [" Why","LMJ","LNJ","YZ"]//2> variable Array//var Arraym = array<string> ()var Arraym =[String] ()//2. Operation of variable array (add and revise)//2.1. Adding elementsArraym.append (" Why") Arraym.append ("YZ") Arraym.append ("LMJ") Arraym.append ("LNJ")//2.2. Deleting an elementArraym.remove (at:0) Arraym//2.3. Modifying elementsarraym[1] =" Why"Arraym//2.4. Get an element of a subscript valuearraym[0]//3. Iterating through an array//Common: Subscript interval traversal forIinch 0.. <arraym.count {print (arraym[i])} forNameinchArraym {print (name)}//not common forIinch 0.. <2{print (arraym[i])} forNameincharraym[0.. <2] {print (name)}//4. Merging of arrays//For name in array {//arraym.append (name)//}//Arraym//in Swift if the two array types are exactly the same, you can add to mergeLet Resultm = Arraym + array

Swift Learning Day Sixth: arrays

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.