What are the arrays and common methods in Swift
This article mainly introduces the array and commonly used methods in Swift, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.
1. Create an array / / create an integer array var array1: [Int] = [] / / [] var arrya2: Array = [1,2,3] var arryaInt = [1,2,3] / / [1,2,3] var array3 = Array (arrayLiteral: 1,2,3) / / [1,2,3] 2. Quickly create an array of repeating elements var array4 = Array (repeating: "swift", count: 3) / / ["swift", "swift", "swift"] var array5 = Array (repeating: 1001, count: 3) / / [1001, 1001, 1001] 3. Array addition / / two arrays of the same type add var array6 = [1,2,3] + [4,5,6] / / [1,2,3,4,5,6] 4. Common methods / / add, delete, change and other methods can be used only when the array is declared to be variable Constant array cannot be modified var array = [1, 2, 3, 4, 5, 6, 7 8] print (array.count) / / 8array / determine that the array is an empty array if array.isEmpty {print ("array is empty")} else {print ("array is not empty")} / / access the element var ele = array [1] / / 2max / intercept the new array var subArray = array [1. 2] / / [2] by subscript 3] / / get the first element, var firstEle = array.first / / 1 _ var firstEle / get the last element, var lastEle = array.last / / 8 _ impulse / modify the element corresponding to the subscript array [1] = 22array / / [1, 22, 3, 4, 5, 6, 7, 8] / / modify the specified range of elements Array [0.. 2] = [1, 2, 3] / / [1, 2] 3] / / append a single element array.append (9) / / [1, 2, 3, 4, 5, 6, 7, 8, 9] / / append a set of elements array.append (contentsOf: [10, 11, 12]) / / [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] / / insert a single element array.insert (0, at: 0) / / at the specified location 4, 5, 6, 7, 8, 9, 10, 11, 12] / / insert a set of elements array.insert (contentsOf: [- 3,-2,-1], at: 0) / / [- 3,-2,-1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 at the specified location 12] / / remove the specified element array.remove (at: 1) / /-2 move / remove a set of elements array.removeSubrange (0.2) / / [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] / remove the first element array.removeFirst () / / 1 move / remove the last element array.removeLast () / / 12 move / remove the first few elements array.removeFirst (3) / / [5 6, 7, 8, 9, 10, 11] / remove the last several elements array.removeLast (3) / / [5,6,7,8] / / replace the specified range of elements array.replaceSubrange (0.3, with: [1,2,3,4]) / / [1,2,3] 4] / / decide to include the specified element if array.contains (3) {print ("array contains 3")} / / remove all elements array.removeAll () / / [] var sortArr = [2, 1, 3,-1] / / sort sortArr.sorted (by:) / / [3, 2, 1 -1] / / get the maximum value of the array sortArr.min () / /-1 move / get the minimum value of the array sortArr.max () / / 35. Array traversal let arr = [11,22,33] for item in arr {print (item)} / / print the subscript of the array and the corresponding elements for item in arr.enumerated () {print (item) / / (offset: 0, element: 11) (offset: 1, element: 22) (offset: 2, element: 33)} / / subscript traversal for index in arr.indices {print (arr)} Thank you for reading this article carefully I hope the article "what are the arrays and common methods in Swift" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!