Explanation of Swift4.0 Array and explanation of swift4.0array
An Array is a string of ordered elements of the same type. The set elements in an Array are ordered and can be repeated. In Swift, the Array type is Array, which is a generic set. Arrays are divided into variable arrays and immutable arrays. let-modified arrays are immutable arrays, and var-modified arrays are variable arrays.
1. Initialize an empty array (type: [data type] ()
1. Create an empty array with an integer
Let array = [Int] ()
Here, the array variables are let's rhetoric. The array is an unchangeable array and can only be accessed and cannot be modified.
Var array = [Int] ()
Here, the array variable is called var. The array is a variable array and can be modified dynamically.
2. Create Any array that can store Any type [Any] ()
// Create a variable Array (which can store Any type of data) var arr: Array = [Any] () // create a variable Array (which stores the string type) // var arr1: [String] = [String] () // 1 Add the element appped () to the array // 1.1 add an element arr to the array. append ("xiaoming") print ("arr = \ (arr)") arr. append (1) print ("arr = \ (arr)"); // 1.2 add an Array to the Array let addArr: Array =, "tianjia"] as [Any] arr. append (addArr) print ("addArr = \ (arr)") // 1.3 Use the addition assignment operator (+ =) you can also add elements directly behind the array, but the added elements must be arrays in the form of the + = Operator, the right must be an array, even if there is only one element To be an array, in this way, you can add multiple values to the array at a time. let addArr1 = [6, 5] as [Any] arr + = addArr1 print ("arr = \ (arr )") // 1.4 Insert the arr element into the array. insert ("inserted element", at: 1) // 2. remove the element from the array // 2.1 remove the specific element arr. remove (at: 0) print ("removeArr = \ (arr)") // 2.2 remove all elements of the array arr. the removeAll () // removeAll method accepts a parameter that allows you to retain the array capacity when clearing the array. The default value of this parameter is false, which sets the capacity of the array to 0. If you want to maintain the capacity, refer to the following code: // var originalCapacity = arr. capacity // originalCapacity = 12 // arr. removeAll (keepingCapacity: true) // var newCapacity = arr. capacity // newCapacity = 12 // The code above shows that emptyArray can store 12 values before the memory needs to be re-allocated. However, with removeAll (), newCapcity is equal to 0. // 2.3 remove the first element of the array // arr. removeFirst () // 2.4 remove the last element of the array // arr. removeLast () // in these removal methods, the removed data is returned. // 3. array length let count = arr. count print ("arrCont = \ (count)") // 4. modify arr [1] = "xiugai" print ("xiuGai = \ (arr)") // 5. access the element let item = arr [1] print ("item = \ (item)") in the array )")
Var stringArr: [String] = ["xiaoming", "tianya", "xiaoming ", "tiantian"] // traverses the array for item in stringArr {print ("iteem ==\ (item)")} for item in 0 .. <stringArr. count {print ("iteem ==\ (item)")} // set the traversal interval for item in stringArr [0... 2] {print ("iteem ==\ (item )")}