Swift学习笔记第二节:数组类型

1、代码

import Foundation

let arr1 = [1, 2, 3, 4, 5, 6]
let arr2 = ["a", "b", "c", "d", "e", "f"]
let arr3 = [true, false, true]
let arr4: Array<Int> = [1, 2, 3]
let arr5: [Int] = [1, 2, 3]
let arr6 = Array<Int>([1, 2, 3])
let arr7 = Array(1...3)
let arr8 = Array(repeating: 1, count: 5)

let length = arr1.count
let isEmpty = arr1.isEmpty
let firstElement1 = arr2[0]
let firstElement2 = arr2.first ?? ""
let endElement1 = arr2[arr2.count - 1]
let endElement2 = arr2.last ?? ""
let randomElement = arr2.randomElement()
let section1 = arr1[0...2]
let section2 = arr1[2...]
let section3 = arr1[...5]

print("""
定义1: \(arr1)
定义2: \(arr2)
定义3: \(arr3)
定义4: \(arr4)
定义5: \(arr5)
定义6: \(arr6)
定义7: \(arr7)
定义8: \(arr8)
长度: \(length)
是否为空: \(isEmpty)
第一个元素1: \(firstElement1)
第一个元素2: \(firstElement2)
最后一个元素1: \(endElement1)
最后一个元素2: \(endElement2)
随机元素: \(randomElement ?? "")
区间取值1: \(section1)
区间取值2: \(section2)
区间取值3: \(section3)
""")

for item in arr2 {
    print("数组遍历的值: \(item)")
}

for (index, value) in arr2.enumerated() {
    print("索引\(index)对应的值是\(value)")
}

arr2.forEach{ item in
    print("数组遍历的值: \(item)")
}

var arr = [1, 2]
arr.append(4)
print("数组插入单条数据: \(arr)")
arr += [5]
print("数组插入单条数据: \(arr)")
arr.append(contentsOf: [6, 7])
print("数组插入多条数据: \(arr)")

arr.insert(3, at: 2)
print("数组指定位置插入单条数据: \(arr)")
arr.insert(contentsOf: [97, 98, 99], at: 2)
print("数组指定位置插入多条数据: \(arr)")
arr.insert(contentsOf: 33...36, at: 2)
print("数组指定位置插入多条数据: \(arr)")

arr.replaceSubrange(2...8, with: [22, 33])
print("数组替换某一区间数据: \(arr)")
arr.remove(at: 2)
print("数组删除某一数据: \(arr)")
arr.removeFirst()
print("数组删除第一条数据: \(arr)")
arr.removeLast()
print("数组删除第后一条数据: \(arr)")
arr.removeSubrange(1...2)
print("数组删除某一区间数据: \(arr)")
arr.removeAll()
print("数组删除所有数据: \(arr)")

arr = [1, 2, 3, 4, 3, 5, 6]
print("数组是否包含某数据数据: \(arr.contains(3))")
let findFirstvalue = arr.first(where: {$0 > 3})
print("数组第一个符合条件的数据的值: \(findFirstvalue ?? -1)")
let findFirstIndex = arr.first(where: { v in v > 2 })
print("数组第一个符合条件的数据的索引: \(findFirstIndex ?? -1)")
print("数组最大值: \(arr.max() ?? -1)")
print("数组最小值: \(arr.min() ?? -1)")

let optionArr = ["ZhangSan", "LiSi", "WangWu"]
let lowerArr = optionArr.map{ item in item.lowercased() }
print("数组值转小写: \(lowerArr)")
let upperArr = optionArr.map{ item in item.uppercased() }
print("数组值转大写: \(upperArr)")

var numArr = [99, 22, 44, 11, 33, 77]
numArr.reverse()
print("数组反转: \(numArr)")
numArr.sort()
print("数组升序排序: \(numArr)")
numArr.sort(by: >)
print("数组降序排序: \(numArr)")
numArr.shuffle()
print("打乱数组排序: \(numArr)")

let str = "asd+fgh-qwe/zxc,vbn=rty"
let str2arr1 = str.split(whereSeparator: { v in
        v == "+" || v == "-" || v == "/" || v == "," || v == "="
})
print("字符串转数组1: \(str2arr1)")
let str2arr2 = str.split(whereSeparator: { v in
    [ "+", "-", "/", ",", "=" ].contains(v)
})
print("字符串转数组2: \(str2arr2)")

let arr2str1 = str2arr2.joined()
print("数组转字符串1: \(arr2str1)")
let arr2str2 = str2arr2.joined(separator: "-")
print("数组转字符串2: \(arr2str2)")

2、运行结果

定义1: [1, 2, 3, 4, 5, 6]
定义2: ["a", "b", "c", "d", "e", "f"]
定义3: [true, false, true]
定义4: [1, 2, 3]
定义5: [1, 2, 3]
定义6: [1, 2, 3]
定义7: [1, 2, 3]
定义8: [1, 1, 1, 1, 1]
长度: 6
是否为空: false
第一个元素1: a
第一个元素2: a
最后一个元素1: f
最后一个元素2: f
随机元素: a
区间取值1: [1, 2, 3]
区间取值2: [3, 4, 5, 6]
区间取值3: [1, 2, 3, 4, 5, 6]
数组遍历的值: a
数组遍历的值: b
数组遍历的值: c
数组遍历的值: d
数组遍历的值: e
数组遍历的值: f
索引0对应的值是a
索引1对应的值是b
索引2对应的值是c
索引3对应的值是d
索引4对应的值是e
索引5对应的值是f
数组遍历的值: a
数组遍历的值: b
数组遍历的值: c
数组遍历的值: d
数组遍历的值: e
数组遍历的值: f
数组插入单条数据: [1, 2, 4]
数组插入单条数据: [1, 2, 4, 5]
数组插入多条数据: [1, 2, 4, 5, 6, 7]
数组指定位置插入单条数据: [1, 2, 3, 4, 5, 6, 7]
数组指定位置插入多条数据: [1, 2, 97, 98, 99, 3, 4, 5, 6, 7]
数组指定位置插入多条数据: [1, 2, 33, 34, 35, 36, 97, 98, 99, 3, 4, 5, 6, 7]
数组替换某一区间数据: [1, 2, 22, 33, 3, 4, 5, 6, 7]
数组删除某一数据: [1, 2, 33, 3, 4, 5, 6, 7]
数组删除第一条数据: [2, 33, 3, 4, 5, 6, 7]
数组删除第后一条数据: [2, 33, 3, 4, 5, 6]
数组删除某一区间数据: [2, 4, 5, 6]
数组删除所有数据: []
数组是否包含某数据数据: true
数组第一个符合条件的数据的值: 4
数组第一个符合条件的数据的索引: 3
数组最大值: 6
数组最小值: 1
数组值转小写: ["zhangsan", "lisi", "wangwu"]
数组值转大写: ["ZHANGSAN", "LISI", "WANGWU"]
数组反转: [77, 33, 11, 44, 22, 99]
数组升序排序: [11, 22, 33, 44, 77, 99]
数组降序排序: [99, 77, 44, 33, 22, 11]
打乱数组排序: [99, 22, 33, 77, 44, 11]
字符串转数组1: ["asd", "fgh", "qwe", "zxc", "vbn", "rty"]
字符串转数组2: ["asd", "fgh", "qwe", "zxc", "vbn", "rty"]
数组转字符串1: asdfghqwezxcvbnrty
数组转字符串2: asd-fgh-qwe-zxc-vbn-rty

你可能感兴趣的:(Swift,swift,学习,笔记)