A collection of arrays of the first knowledge of Swift collection

Source: Internet
Author: User

Array Collection

Basic operations:

Array Collection definition: A collection of elements of the same type that are ordered by a sequence

Basic declaration of an array:

1, var strudentlist:array<int>; Declares a strudentlist array, the type of the array element is int

2, var strudentlist: [Int]; A lazy strudent list array declaration, array element type int type

Initialization of the array

1, var strudentlist:array<int>= [10,10,10]//Declaration initialization Studentlist Type

2, var strudentlist = [10,101]//According to the type inference ability of swift language to declare an array of studentlist, array element type is Int type.

3, var strudentlist = array<int> ()//Declare a null value of the strudentlist array, the type of the array element is INT type

4, var strudentlist = [String] ()//Declare a controlled strudentlist array, array element type is String type


We can use the two keyword Var and let when declaring an array, and when using Var, the value of the array can be changed later in the program, and the array cannot be changed after it has been declared.


Array element manipulation: Add, delete, change, insert Element

add element

Example: var strudentlist:array<string> = ["Zhang San", "Jack"];

1. Add an element at the end of the array

Strudentlist.append ("10 Yuan");

2. Add multiple elements at the end of the array

Strudentlist + = ["Mark", "R"];

inserting elements

Strudentlist.insert ("Flying Fish", atindex:num)//num is the position of the array to be inserted, note here that the position of the array is calculated from zero, such as [10,10] then his next table technique is 0,1

Delete Element

Let Names=strudentlist.removeatindex (num); NUM is the following table to delete the array elements, using this method of deletion, the method can return the contents of the deleted element, and if this element is not required we can directly strudentlist.removeatindex (num)


Traversal of an array

Array traversal AH there are three ways to use

1. For in output array element

For item in Studentlist {println (item); }

2. Output array subscript and element

For (ID, name) in enumerate (studentlist) {if (id! = studentlist.count-1) {print ("Item \ (ID): \ (name),")        ;        }else{println ("Item \ (ID): \ (name)"); }    }

3. Use this method to output

for (var i:int = 0; i < strudentlist.count-1; i++) {if (i! = strudentlist.count-2) {print ("Item \ (i): \ (St    Rudentlist[i]), ");    }else{println ("Item \ (i): \ (Strudentlist[i])"); }}


This article is from the "10 Yuan" blog, please be sure to keep this source http://tenyuan.blog.51cto.com/9401521/1620718

A collection of arrays of the first knowledge of Swift collection

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.