Swift basic usage-arrays Array

Source: Internet
Author: User

Array simple usage

//------------------------------------------------------------------------------  //1. Array Definitions//1> uses [] to quickly define an array, with each array element used, delimited//the data elements in the 2> array can be of different typesvar array = ["Hello","Swift",1,1.2]    //------------------------------------------------------------------------------  //2. If you want the data stored in the array to be of the same data type//You can specify the data type in the array when you define it, with the following code://  //1> Array1 is an array that allows only strings to be storedvar array1:[string] Array1= ["Hello","Swift"]    //2> Array2 is an array that is allowed to hold only intvar array2:[int] = [1,2,3,4,5]    //------------------------------------------------------------------------------  //3. Creating an array with generics//type in <> when using a generic definition array to specify the type of data stored in the array//  //1> Array3 is an array that is allowed to hold only int, and () instantiates an arrayvar array3 = array<int>()    //2> When instantiating an array, you can use the constructor of the array to specify the initial members of the array//instantiate an array that contains three integers of 20var nums = array<int> (count:3, Repeatedvalue: -)    //------------------------------------------------------------------------------  //4. Array Common operations//1> Append elementNums.append ( +)    //You can also use operators to add elements directly to an arrayNums + = -    //2> inserting elementsNums.insert ( +, Atindex:0) Nums.insert (Ten, Atindex:3)    //You can enter the variable/constant name directly in playground to view the current contentNums//3> Delete the specified position elementNums.removeatindex (2)    //using variables to record the nums array, essentially making a memory copy, subsequent changes to the array, will not affect the contents of the variable arraysvar resultnums =nums var a= nums[3] Var b= -var c= A +b//4> Delete End elementnums.removelast () nums//5> Delete all elements, but reserve storage spaceNums.removeall (keepcapacity:true)    //Displays the contents of the Resultnums, and the contents of the array are not affectedresultnums nums//6> Array Merging//To save an array of the same data type, you can use + direct mergevar Mergearray = array2 + resultnums

RELATED links:

Swift basic usage-arrays Array

Swift basic usage-arrays Array

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.