,敢教日月换新天。为有牺牲多壮志

[Swift]Array(数组)扩展

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ 
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10403543.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

扩展Array

复制代码
 1 extension Array where Element : Equatable {    
 2     //获取数组中的指定元素的索引值
 3     //Parameter item: 元素
 4     //Returns: 索引值数组
 5     public func indexes(_ item: Element) -> [Int] {
 6         var indexes = [Int]()
 7         for index in 0..<count where self[index] == item {
 8             indexes.append(index)
 9         }
10         return indexes
11     }    
12     
13     //获取元素首次出现的位置
14     //Parameter item: 元素
15     //Returns: 索引值
16     public func firstIndex(_ item: Element) -> Int? {
17         for (index, value) in lazy.enumerated() where value == item {
18             return index
19         }
20         return nil
21     }    
22     
23     //获取元素最后出现的位置
24     //Parameter item: 元素
25     //Returns: 索引值
26     public func lastIndex(_ item: Element) -> Int? {
27         return indexes(item).last
28     }
29     
30     //删除数组中的指定元素
31     //Parameter object: 元素
32     public mutating func remove(_ object:Element) -> Void {
33         for idx in self.indexes(object).reversed() {
34             self.remove(at: idx)
35         }
36     }
37 }
复制代码

测试代码:

1 var arr:[Int] = [1,2,3,4,5,5,6,7,7,8,9,10]
2 print(arr.firstIndex(5))
3 //Prnt Optional(4)
4 print(arr.lastIndex(7))
5 //Prnt Optional(8)
6 arr.remove(7)
7 print(arr)
8 //Prnt [1, 2, 3, 4, 5, 5, 6, 8, 9, 10]

 扩展数组,二分法插入:

复制代码
 1 private extension Array where Element: Comparable {
 2     private func binarySearchIndex(for element: Element) -> Int {
 3         var min = 0
 4         var max = count
 5         while min < max {
 6             let index = (min+max)/2
 7             let other = self[index]
 8             if other == element {
 9                 return index
10             } else if other < element {
11                 min = index+1
12             } else {
13                 max = index
14             }
15         }
16         return min
17     }
18     
19     mutating func binaryInsert(_ element: Element) {
20         insert(element, at: binarySearchIndex(for: element))
21     }
22 }
复制代码

测试代码:

1 var arr:[Int] = [1,2,3,4,5,6,7,8,9]
2 arr.binaryInsert(5)
3 print(arr)
4 //Print [1, 2, 3, 4, 5, 5, 6, 7, 8, 9]

 

posted @   为敢技术  阅读(593)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
2°
西南风
2级
空气质量
相对湿度
62%
今天
2°/16°
周二
7°/19°
周三
多云
7°/19°