Swift-array (Array)

Source: Internet
Author: User

The Swift array is used to store a sequential list of values of the same type. Swift is strictly checked to not allow different types of values to be in the same array

Declares an array

var Somearray ==30)// Declare an array of three capacities with an initial value of 0var someints:[int] = [ Ten  - +] // declares an int type whose initial value is an array within brackets

You can use the following banner to retrieve the corresponding value from the array, passing the value of the index in parentheses behind the array name, as follows:

var somevar = Somearray[index]

Here, the exponent starts at 0, which means that the first element can be accessed using index zero, and the second element can be accessed by using index 1, other similar. Let's take a look at the following examples of creating, initializing, and accessing arrays:

3 Ten  = someints[0"Value of first element is \ (somevar)"  " Value of second element is \ (Someints[1]) "  "Value of third element is \ (someints[2])" )

When the above code is compiled and executed, it produces the following results:

 is Ten  is ten
Modifying an array

You can use the Append () method or the addition assignment operator (+ =) to add a new item to the end of the array, where you first create an empty array, and then add a new element to the array, as shown in:

Import Cocoavar someints= [Int] ()//Initializes an empty array of type intSomeints.append ( -)//AddSomeints.append ( -)//AddSomeints + = [ +]//Add Avar somevar= someints[0]println ("Value of first element is \ (Somevar)") println ("Value of second element is \ (Someints[1])") println ("Value of third element is \ (someints[2])")
 is  -   is

You can modify the values in an array element by assigning values directly to an already existing array element

Import Cocoavar someints=[Int] () someints.append ( -) Someints.append ( -) someints+= [ +]//Modify last elementsomeints[2] = -var somevar= someints[0]println ("Value of first element is \ (Somevar)") println ("Value of second element is \ (Someints[1])") println ("Value of third element is \ (someints[2])")
 is  -   is

You can use the for-in loop iteration progression, in the following example, the entire set value of the array, as shown in:

= [String] () somestrs.append ("Apple") somestrs.append ("  Amazon"+ = ["Google"]for in  somestrs {   println (item)}

When the above code is compiled and executed, it produces the following results:

Appleamazongoogle

The

can also use the enumerate () function, as shown in the following example, which returns the index and its corresponding value:

 import cocoavar somestrs  = [String] () Somestrs.append (  apple   " ) Somestrs.append ( "  amazon   ) somestrs  + = [ " google  "   for  (Index, item) in   enumerate (somestrs) {println (  value at index = \ (index) is \ (item)   )}  

When the above code is compiled and executed, it produces the following results:

Value at index = 0 are applevalue at index = 1 are amazonvalue at index = 2 is Google

  

Two arrays added

Use the addition operator (+) to add an array of the same type, which will produce a new array that is derived from two array values after the combined array, as follows:

Import Cocoavar Intsa = [int] (count:2, repeatedvalue:2) var intsb = [Int] (Count:3, repeatedvalue:1) var intsc = Intsa + in Tsbfor item in INTSC {   println (item)}

  

When the above code is compiled and executed, it produces the following results:

22111

  

Count Property

You can use the read-only calculation (count) array property to find out the number of elements in the arrays shown below:

Import Cocoavar Intsa = [int] (count:2, repeatedvalue:2) var intsb = [Int] (Count:3, repeatedvalue:1) var intsc = Intsa + in  Tsbprintln ("total items in Intsa = \ (intsa.count)") println (' total items in intsb = \ (intsb.count) ') println ("Total items in INTSC = \ (intsc.count) ")

  

When the above code is compiled and executed, it produces the following results:

inch 2  in3 in 5

Empty property

Use the empty attribute of the read-only group (IsEmpty) to find out if an array is empty, as shown in:

= [int] (count:22= [int] (count:31= [int] () println ( " intsa.isempty = \ (intsa.isempty) " ) println ("intsb.isempty = \ (intsb.isempty)") println (  "intsc.isempty = \ (intsc.isempty)")

When the above code is compiled and executed, it produces the following results:

Intsa.isempty = Falseintsb.isempty = Falseintsc.isempty = True

  

Swift-array (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.