Swift - 18 - 数组的基础操作

时间:2023-03-08 17:18:15
//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

var array = ["A", "B", "C", "D", "E", "F"]

let arrayCount = array.count
array.isEmpty array.append("G")
array += ["H"]
array += ["I", "J"] array.insert("Rinpe", atIndex: 0)
array.insert("Bobo", atIndex: 1)
array.removeAtIndex(0) // 返回被删除的元素
array.removeLast()
array.removeFirst()
array
array[0] = "AA"
//: 通过区间获取出来的还是一个数组, 所以赋值的时候还是要用数组
array[1...3] = ["BB", "CC", "DD"]
array[4...5] = ["hello"]
array //: 遍历数组
for index in 0..<array.count {
print(array[index])
} for item in array {
print(item)
} for (index, item) in array.enumerate() {
print("index:\(index), item:\(item)")
}