Information about the 15.swift array

Source: Internet
Author: User
Tags array definition

/********************** Array definition **************************/

array : Array represents data

// Let decorated identifiers are immutable groups ( cannot be modified after element is determined )

The //var modifier identifier is a mutable array ( elements can be added and removed )

Note :

//1> definition array is used [], and does not need to add @

//2> Typically, an array is a generic collection , and all typically specifies which elements can be stored in the array


// immutable variable Group One : defines an array in which all strings are stored

Let names: Array<String> = ["Why", "LMJ", "Lnj", "YZ" ]


// immutable Arrays Two : defines an array in which all strings are stored

let names1: [String] = ["Why", "LMJ", "Lnj", "YZ"]


// immutable Arrays Two : defines an array in which all strings are stored

Let names2 = ["Why", "LMJ", "Lnj", "YZ"]

// No element can be added

Names.append ("22")

// array to hold multiple data types in the notation

Let array: [anyobject] = ["Why", 1.88]


// variable array

// Create variable array mode one :

var array1: [String] = Array()


// common variable array mode two :

var array2 = [String] ()




/********************** array Operation **************************/

adding elements : through the Append method

Array1. Append ("Why")

Array1. Append ("LNJ")


// Delete element

Let removestring = array1. Removeatindex (0)

Array1


// Modify Element

Array1[0] = "LMJ"

Array1


// get the values in the array

Let str = array1[0]


// Gets the number of elements in the array

Let count = array1. Count


/********************** Array Traversal **************************/


// traverse mode one :

for i in 0.. <names. Count {

Print (names[i])

}


traversal mode two : Forin

for item in names {

Print (item)

}


// traversal mode three : interval traversal

for item in names[0.. <2] {

Print (item)

}



/********************** Merging **************************/

//1. Merge of the same type

Let names5 = ["LMJ", "LNJ"]

Let names6 = ["YZ", "why"]


Let Names7 = names5 + names6


//2. Different types of merges : not additive

var array5 = ["Why", ]

Let array6 = [1.88, 60.5]


Let Array7 = Array5 + array6

for item in array6 {

array5. Append (item)

}


Array5


// Note : It is not recommended to store multiple elements in an array


Information about the 15.swift array

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.