Collection of arrays in Swift-B

Source: Internet
Author: User

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

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.