SoFunction
Updated on 2025-03-02

Detailed explanation of Swift4.0 Array array

Introduction to arrays

An array (Array) is an ordered set of elements composed of the same type of elements. The set elements in the array are ordered and can appear repeatedly. In Swift, the array type is Array, which is a generic collection. Arrays are divided into: mutable arrays and immutable arrays. The arrays modified by let are immutable arrays, and the arrays modified by var are mutable arrays.

Initialization of arrays

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

1. Create an empty array for shaping

let  array = [Int] ()

Here array array variable is let rhetorical. The array array is an immutable array that can only be accessed, cannot be modified.

var  array = [Int] ()

Here array variables are verified by var. array arrays are variable arrays that can be modified dynamically

2. Create any array that can store any type [Any] ()

Use of arrays

 //Create a variable array (can store any type of data) var arr:Array = [Any]()
 //Create a variable array (storage string type)// var arr1:[String] = [String]()
 
// 1 Add elements to the array appped() function 
 //1.1 Add an element to the array ("xiaoming")
 print("arr == \(arr)")
 (1)
 print("arr = \(arr)");
 
 //1.2 Add an array to the array let addArr:Array = [1,2,"tianjia"] as [Any]
 (addArr)
 print("addArr = \(arr)")
 
 //1.3 Using the addition assignment operator (+=) can also add elements directly after the array, but the added elements must be in the form of an array. When using the += operator, the right one must be an array, even if there is only one element, it must be an array. In this way, you can add multiple values ​​to the array at once. let addArr1 = [6,5] as [Any]
 arr += addArr1
 print("arr = \(arr)")
 
 //1.4 Insert elements into the array ("Inserted Element", at: 1)
 
 //2. Remove elements from the array //2.1 Remove specific elements (at: 0)
 print("removeArr = \(arr)")
 //2.2 Remove all elements of the array ()
// The removeAll method accepts a parameter that allows whether to maintain the capacity of the array when clearing the array. The default value of this parameter is false, that is, the capacity of the array is set to 0.  If you want to maintain capacity, you can refer to the following code:// var originalCapacity =  //
// originalCapacity = 12
// (keepingCapacity: true)
// var newCapacity =  //
// newCapacity = 12
// From the above code, we can see that emptyArray can store 12 values ​​before reallocating memory. However, using removeAll() and newCapcity is equal to 0. 
 //2.3 Remove the first element of the array// ()
 //2.4 Remove the last element of the array// ()
// All removed data are returned in these removal methods 
// 3. Length of array let count = 
 print("arrCont = \(count)")
 
// 4. Modification of elements in array arr[1] = "xiugai"
 print("xiuGai = \(arr)")
 
// 5. Access elements in the array let item = arr[1]
 print("item = \(item)")

Iterate through the array

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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.