初始化一个数组
var array1=[String]()
var array2=Array<String>()
var array3=Array(repeating: "z", count: 5)
var aray4=[Int](0...7)
数组的遍历
var numbers=[Int](0...7)
numbers.forEach { (num) in
print(num*10)
}
数组的索引和值
for (index,num) in numbers.enumerated() {
print("\(index)+\(num)")
}
for i in numbers.indices {
print("\(i)")
}
查找操作
var array5=[10,20,45,30,98,101,30,4];
print(array5.allSatisfy({$0>10}))
print(array5.first)
print(array5.last)
print(array5.first(where: {$0>25}))
print(array5.last(where: {$0>25}))
print(array5.firstIndex(of: 30))
print(array5.lastIndex(of: 20))
print(array5.max())
print(array5.min())
数组的添加和删除
var numbers2=[Int](2...7)
numbers2.append(100)
print(numbers2)
numbers2.append(contentsOf: 101...105)
print(numbers2)
numbers2.insert(1, at: 0)
numbers2.insert(contentsOf: 101...103, at: 0)
numbers2.remove(at: 1)
numbers2.removeFirst()
numbers2.popLast()
numbers2.remove(at: 1)
numbers2.removeFirst()
numbers2.removeFirst(2)
numbers2.removeLast(2)
numbers2.removeSubrange(1...2)
numbers2.removeAll()
numbers2.removeAll(keepingCapacity: true)
ArraySlice
let array6=[5,2,10,1,0,100,46,99];
array6.dropFirst()
array6.dropFirst(3)
array6.dropLast()
array6.dropLast(3)
数组元素的随机化:
字符串数组拼接:
|