Array

2023/1/29

# 概念

  • 数组有序数据的集。
  • 集合无序无重复数据的集。
  • 字典无序键值对的集。

# Array

  • 初始化固定长度数组
    let b = Array(repeating: 0, count: 10000);
    print(b); // [0, 0, ...]
    
    1
    2
  • 初始化特定类型的空数组
    let b: [String] = [];
    print(b); // []
    
    1
    2

# 拼接

  • 必须要同类型才能拼接
let a: [Double] = [1, 2, 3, 4, 5, 6, 7]

let b = [8.0, 9.0];
print(a + b); // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
1
2
3
4

# count

let a: [Double] = [1, 2, 3, 4, 5, 6, 7]
print(a.count); // 7
1
2

# 相等

  • 可以用==elementsEqual判断两个数组的元素和顺序相等
var a = [1, 2, 3, 4, 5, 6, 7]
var b = [1, 2, 3, 4, 5, 6, 7]

print(a == b) // true
print(a.elementsEqual(b)) // true
1
2
3
4
5

# 增删改查

# contains

  • 单值包含
    var a = [1, 2, 3, 4, 5, 6, 7]
    
    let b = a.contains(3)
    print(b); // true
    
    1
    2
    3
    4
  • 复杂条件包含
    var a = [1, 2, 3, 4, 5, 6, 7]
    
    let b = a.contains(where: {$0 > 8})
    print(b); // false
    
    1
    2
    3
    4

# starts

var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.starts(with: [1, 2, 3])

print(b); // true
1
2
3
4
5

# first

  • 第一个符合计算函数的值
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.first(where: {$0 > 2}) ?? -1
print(b); // 3
1
2
3
4

# last

  • 最后一个符合计算函数的值
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.last(where: {$0 < 5}) ?? -1
print(b); // 4
1
2
3
4

# lastIndex

  • 最后一个匹配下标
var a = [1, 2, 3, 4, 5, 6, 3]

let b = a.lastIndex(of: 3) ?? -1
print(b); // 6
1
2
3
4

# firstIndex

  • 第一个匹配下标
var a = [1, 2, 3, 4, 5, 6, 3]

let b = a.firstIndex(of: 3) ?? -1
print(b); // 2
1
2
3
4

# append

var a: [Double] = [1, 2, 3, 4, 5, 6, 7]
a.append(8)
print(a); // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
1
2
3

var a: [Double] = [1, 2, 3, 4, 5, 6, 7]
a += [8]
print(a); // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
1
2
3

# 区间替换

  • 单个字符替换
    var a = [1, 2, 3, 4, 5, 6, 7]
    a[3] = 10
    print(a); // [1, 2, 3, 10, 5, 6, 7]
    
    1
    2
    3
  • 区间替换(相当于JS的splice),删除指定位数并插入数组
    var a = [1, 2, 3, 4, 5, 6, 7]
    a[3...4] = [10, 11, 199]
    print(a); // [1, 2, 3, 10, 11, 199, 6, 7]
    
    1
    2
    3

# replaceSubrange

  • 写法没有区间替换写法简洁
var a = [1, 2, 3, 4, 5, 6, 7]

a.replaceSubrange(0...2, with: [0, 0])
print(a); // [0, 0, 4, 5, 6, 7]
1
2
3
4

# swapAt

  • 两个元素换位置
var a = [1, 2, 3, 4, 5, 6, 7]

a.swapAt(2, 3)

print(a); // [1, 2, 4, 3, 5, 6, 7]
1
2
3
4
5

# partition

  • 查询不满足给定条件的数量
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.partition(by: {$0 > 1})

print(b); // 1
1
2
3
4
5

# insert

  • 单个字符插入
    var a = [1, 2, 3, 4, 5, 6, 7]
    a.insert(100, at: 3)
    print(a); // [1, 2, 3, 100, 4, 5, 6, 7]
    
    1
    2
    3
  • 多个字符插入
    var a = [1, 2, 3, 4, 5, 6, 7]
    a.insert(contentsOf: [100, 101], at: 3)
    print(a); // [1, 2, 3, 100, 101, 4, 5, 6, 7]
    
    1
    2
    3
  • 头部插入
    var a = [1, 2, 3, 4, 5, 6, 7]
    a.insert(0, at: 0)
    print(a); // [0, 1, 2, 3, 4, 5, 6, 7]
    
    1
    2
    3

# remove

  • 移除指定下标项
var a = [1, 2, 3, 4, 5, 6, 7]
a.remove(at: 3)
print(a); // [1, 2, 3, 5, 6, 7]
1
2
3

# removeSubrange

  • 移除下标区间
var a = [1, 2, 3, 4, 5, 6, 7]
a.removeSubrange(1...3)
print(a); // [1, 5, 6, 7]
1
2
3

# removeAll

var a = [1, 2, 3, 4, 5, 6, 7]
a.removeAll(where: {$0 < 3})
print(a); // [3, 4, 5, 6, 7]
1
2
3

# removeLast

  • 移除最后一个
var a = [1, 2, 3, 4, 5, 6, 7]
a.removeLast()
print(a); // [1, 2, 3, 4, 5, 6]
1
2
3

# removeFirst

  • 移除第一个
var a = [1, 2, 3, 4, 5, 6, 7]
a.removeFirst()
print(a); // [2, 3, 4, 5, 6, 7]
1
2
3

# suffix

  • 获取末尾固定位数新数组
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.suffix(3)

print(b); // [5, 6, 7]
1
2
3
4
5

# 遍历

# for-in

var a = [1, 2, 3, 4, 5, 6, 7]

for i in a {
  print(i); // 1234567
}
1
2
3
4
5

# enumerated

  • 转化成区间[(idx, val)...]
var a = [1, 2, 3]

for (idx, val) in a.enumerated() {
  print("index是\(idx), value 是\(val)")
}

//index是0, value 是1
//index是1, value 是2
//index是2, value 是3
1
2
3
4
5
6
7
8
9

# indices

var a = [1, 2, 3, 4, 5, 6, 7]

for idx in a.indices {
    print(idx); // 0123456
}
1
2
3
4
5

# forEach

var a = [1, 2, 3, 4, 5, 6, 7]

a.forEach({print($0)}) // 1234567
1
2
3

# 反向遍历

var a = [1, 2, 3, 4, 5, 6, 7]
a.reversed().forEach({print($0)}); // 7654321
1
2
var a = [1, 2, 3, 4, 5, 6, 7]

for i in a.reversed() {
    print(i); // 7654321
}
1
2
3
4
5

# 其他操作

# sort

  • 传入函数返回值确定 $0是否在前面
  • 不改变原数组
var a = [1, 6, 3, 4, 2, 5]

let b = Array(a.sorted(by: {$0 < $1}))
print(b); // [1, 2, 3, 4, 5, 6]
1
2
3
4

# filter

  • 不改变原数组
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.filter({$0 % 2 != 0})
print(b); // [1, 3, 5, 7]
1
2
3
4

# map

  • 不改变原数组
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.map({$0 % 2 == 0 ? "偶" : "奇"})
print(b); // ["奇", "偶", "奇", "偶", "奇", "偶", "奇"]
1
2
3
4

# reduce

  • 不改变原数组
  • 参数与js相反,$0初始值$1计算函数
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.reduce(100, {$0 + $1})
print(b); // 128
1
2
3
4

# compactMap

  • map用法相同,但是返回的一定是非空项,否则就剔除掉
var a = [1, 2, 3, 4, 5, 6, 3]

let b = a.compactMap({$0 > 3 ? $0 : nil})
print(b); // [4, 5, 6]
1
2
3
4

# compactMap

  • map + flat功能
var a = [1, 2, 3, 4, 5, 6]

let b = a.flatMap({Array(repeating: $0, count: $0)})
print(b); // [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6]
1
2
3
4

# reversed

  • 逆转后只是用的原数组引用,如果要生成新数组要用Array
var a = [1, 2, 3, 4, 5, 6]

let b = Array(a.reversed())
print(b); // [6, 5, 4, 3, 2, 1]
1
2
3
4

# max

var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.max()!
print(b); // 7
1
2
3
4

# min

var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.min()!
print(b); // 1
1
2
3
4

# prefix

  • 相当于获取区间,得到subSequence
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.prefix(3)
let c = a[0...2]

print(b); // [1, 2, 3]
print(c); // [1, 2, 3]
1
2
3
4
5
6
7

# randomElement

  • 随机获取数组的一个元素
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.randomElement()!

print(b); // 1
1
2
3
4
5

# shuffled

  • 洗牌函数
var a = [1, 2, 3, 4, 5, 6, 7]

let b = a.shuffled()

print(b); // [5, 3, 2, 4, 6, 7, 1]
1
2
3
4
5
上次更新: 11/1/2024