Welcome to Swift (initial translation and annotations of Apple's official Swift document 22)---148~153 page (chapter III-collection type)

Source: Internet
Author: User

To insert an element at the specified index of an array, you can call the array's insert (Atindex:) Method:

Shoppinglist.insert ("Maple syrup", atindex:0)

Shoppinglist now contains 7 items

"Maple syrup" is now the first item in the list "

The Insert method in the example inserts a new element at the beginning of the array (index 0), and the value of the element is "Maple syrup"

Similarly, you can use the Removeatindex method to remove an element from an array. This method removes the element at the specified index position of the divisor, and returns the removed element (although you may not need the return value):

Let Maplesyrup = Shoppinglist.removeatindex (0)

The item is at index 0 have just been removed

Shoppinglist now contains 6 items, and no Maple syrup

The Maplesyrup constant is now equal to the removed "Maple syrup" string "

When an element in an array is removed, the subsequent elements occupy the empty space after removal. Therefore, the value of the position of index 0 in the array in the example code is once again equal to "Six eggs":

Irstitem = shoppinglist[0]

FirstItem is now equal to "Six eggs"

If you want to remove the elements from the last polygon of the divisor, you can use the Removelast method instead of the Removeatindex method, which avoids querying the Conut property of the array. Similar to the Removeatindex method, the Removelast method Returns the element that was deleted:

Let apples = shoppinglist.removelast ()

The last item in the array has just been removed

Shoppinglist now contains 5 items, and no cheese

The apples constant is now equal to the removed "apples" string "

Iterating over an array (traversal array)

You can use the for-in loop to variable the entire array:

For item in Shoppinglist {

println (item)

}

Six eggs

Milk

Flour

Baking Powder

Bananas

If you need to know the value of each index and its corresponding element, you can use the global function enumerate to iterate through the array. The global function enumerate returns a tuple that contains the element values for the index and index in the tuple. You can parse them from a tuple and save them to a temporary constant or variable. Use the constant or variable as part of the traversal:

For (index, value) in enumerate (shoppinglist) {

println ("Item \ (index + 1): \ (value)")

}

Item 1:six Eggs

Item 2:milk

Item 3:flour

Item 4:baking Powder

Item 5:bananas

Creating and Initializing an array (creating arrays and initializing arrays)

Use the initialization syntax to create an empty array of some type (without setting any initial values):

var someints = int[] ()

println ("someints is of type int[] with \ (someints.count) items.")

Prints "someints is of type int[] with 0 items."

Note that the type of the variable someints is int[], because it uses int[] to initialize.

If the type information for the content has been specified (such as a function parameter, or a variable or constant of a type already defined), you can create an empty array using the blank brackets []:

Someints.append (3)

Someints now contains 1 value of type Int

Someints = []

Someints is now a empty array, but is still of type int[]

In swift, the array type also provides an initialization method that uses the provided default value to create a fixed-size array. You need to pass the initialization method the number of elements (count) and the default value of its corresponding type (Repeatedvalue):

var threedoubles = double[] (count:3, repeatedvalue:0.0)

Threedoubles is of type double[], and equals [0.0, 0.0, 0.0]

Because of the type-judging mechanism of swift, when using an initialization method to create an array, you do not have to specify the type that the array is to store, because Swift can determine the type of the array storage values by default values:

var anotherthreedoubles = Array (Count:3, repeatedvalue:2.5)

Anotherthreedoubles is inferred as double[], and equals [2.5, 2.5, 2.5]

Finally, you can use the addition operator to create a new array, and the type of the new array can be judged by the two existing arrays of the addition operator:

var sixdoubles = threedoubles + anotherthreedoubles

Sixdoubles is inferred as double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

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.