Swift 4.0 Array array details

  • 2020-06-01 11:06:24
  • OfStack

Introduction to arrays

An array (Array) is an ordered collection of elements of the same type. The collection elements in an array are ordered and can be repeated. In Swift, the array type is Array, which is a generic collection. Arrays are divided into mutable arrays and immutable arrays. Arrays modified with let are immutable, and arrays modified with var are mutable.

Initialization of an array

1. Initialize an empty array (type :[data type]())

1. Create an empty array of integers

let array = [Int] ()

Here the array array variable is represented by let, and the array array is an immutable array that can only be accessed, not modified

var array = [Int] ()

Here the array array variable is represented by the var rhetoric, and the array array is a mutable array that can be dynamically modified

2. Create any type of array [Any] ()

Use of arrays


 // create 1 1 mutable array (can store any type of data) 
 var arr:Array = [Any]()
 // create 1 Two mutable arrays   (store string type) 
// var arr1:[String] = [String]()
 
// 1  Add elements to the array  appped() function 
 
 //1.1 Add to array 1 An element 
 arr.append("xiaoming")
 print("arr == \(arr)")
 arr.append(1)
 print("arr = \(arr)");
 
 //1.2 Add to array 1 An array 
 let addArr:Array = [1,2,"tianjia"] as [Any]
 arr.append(addArr)
 print("addArr = \(arr)")
 
 //1.3 Using the addition assignment operator ( += You can also add elements directly to the end of the array, but only if the element is in an array   use += When the operator , The right-hand side must be an array , Even if only 1 An element is also an array , And that's the way you can 1 Subincrements an array by multiple values 
 let addArr1 = [6,5] as [Any]
 arr += addArr1
 print("arr = \(arr)")
 
 //1.4 Insert an element into the array 
 arr.insert(" Inserted element ", at: 1)
 
 //2. Remove the elements from the array 
 //2.1 Remove a specific element 
 arr.remove(at: 0)
 print("removeArr = \(arr)")
 //2.2 Remove all elements of the array 
 arr.removeAll()
// removeAll Method accepts 1 Allows you to empty an array to keep its capacity , The default value of this parameter is false, Will array capacity The capacity is set to 0 . If you want to keep the capacity , Please refer to the following code: 
// var originalCapacity = arr.capacity //
// originalCapacity = 12
// arr.removeAll(keepingCapacity: true)
// var newCapacity = arr.capacity //
// newCapacity = 12
//  As can be seen from the above code emptyArray It can be stored before memory needs to be reallocated 12 A value , However, using removeAll(),newCapcity Is equal to the 0 . 
 
 //2.3 Removes the th of the array 1 An element 
// arr.removeFirst()
 //2.4 Remove the end of the array 1 An element 
// arr.removeLast()
//  In each of these removal methods, the removed data is returned 
 
// 3.  The length of the array 
 let count = arr.count
 print("arrCont = \(count)")
 
// 4. Changes to elements in an array 
 arr[1] = "xiugai"
 print("xiuGai = \(arr)")
 
// 5. Access the elements in the array 
 let item = arr[1]
 print("item = \(item)")

Through the array


var stringArr:[String] = ["xiaoming","tianya","xiaoming","tiantian"]
 //  Through the array 
 for item in stringArr {
  print("iteem == \(item)")
 }
 
 for item in 0..<stringArr.count {
  print("iteem == \(item)")
 }
 
 //  Sets the interval to traverse 
 for item in stringArr[0...2] {
  print("iteem == \(item)")
 }

Related articles: