• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift学习笔记(5)--数组

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

数组的下标从0开始计数,相关方法属性涉及到下标时也从0开始计数

1.定义:

//1.可变数组
var cityArray = ["Portland","San Francisco","Cupertino"]
//2.不可变数组
let cityArray2 = ["Portland","San Francisco","Beijing"]
//3.空数组
var animalArray = String[]()
var animalAgeArray = Int[]()

  

2.基本方法:统计总数,判断非空

//1.统计总数
var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
let count = cityArray.count

//2.判断非空
if cityArray.isEmpty {
    println("Empty")
}else{
    println("Not Empty")
}

  

3.遍历

var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]

//1.元素遍历
for animal in animalArray {
    println(animal)
}

//2.带下标遍历
for(index,animal) in enumerate(animalArray) {
    
    println("The \(animal) is at index:\(index)")
    
}

for var i = 0; i < animalArray.count; i++ {
    println(animalArray[i])
}

for i in 0..animalArray.count {
    println(animalArray[i])
}

  

4.添加元素或者子数组

//1.添加单个元素
var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
cityArray.append("Seattle") //函数添加
cityArray += "Seattle"      //运算符重载添加

//2.添加子数组
var cityArrayA: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
var cityArrayB: String[] = ["Vancouver", "Los Angeles", "Eugene"]

cityArrayA += ["Vancouver", "Los Angeles", "Eugene"]  //固定子数组
cityArrayA += cityArrayB;   //变量子数组

  

5.改变数组元素

  注:改变多个元素,如果下标与元素的数量不一致,以下标做多退少补

var cityArray: String[] = ["Portland","San Francisco","Cupertino","Seattle","NewYork"]

//1.改变单个元素
cityArray[0] = "Oregon"


//2.改变多个元素,如果下标与元素的数量不一致,以下标做多退少补
    //2.1 相等时
var cityArray0 = cityArray
cityArray0[0...2] = ["Beijing","Shanghai","Guangzhou"]
println(cityArray0) //[Beijing, Shanghai, Guangzhou, Seattle, NewYork]
    //2.2 下标多于元素时,原数组中多出的下标数据被remove出原数组,即多退
var cityArray2 = cityArray
cityArray2[0...3] = ["Beijing","Shanghai","Guangzhou"]
println(cityArray2) //[Beijing, Shanghai, Guangzhou, NewYork]
    //2.3 下标少于元素时,将多出的元素添加到原数组,即少补
var cityArray3 = cityArray
cityArray3[0...1] = ["Beijing","Shanghai","Guangzhou"]
println(cityArray3) //[Beijing, Shanghai, Guangzhou, Cupertino, Seattle, NewYork]

  

6.删除元素

//1.删除指定元素
var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl", "Beaver"]
animalArray.removeAtIndex(4)
println(animalArray)    //[Dog, Cat, Fish, Owl]

//2.删除最后一个元素
var animalArray2: String[] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray2.removeLast()   //[Dog, Cat, Fish]
println(animalArray2)

//3.删除所有元素
var animalArray3: String[] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray3.removeAll()
println(animalArray3)   //[]

  

注:如果你想保留原来被删除的元素,可以使用let item = array.removeFunction() 来保留  

var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl", "Beaver"]
let item = animalArray.removeAtIndex(4)
println(item)    //Beaver

  

7.排序

基本排序

var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
sort(animalArray)
//The array values are now [Cat, Dog, Fish, Owl]

  

反向重排

var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray.reverse()
//The array values are now ["Owl", "Fish", "Cat", "Dog"]

  

自定义快速排序

func quicksort_swift(inout a:Int[], start:Int, end:Int) {
    if (end - start < 2){
        return
    }
    var p = a[start + (end - start)/2]
    var l = start
    var r = end - 1
    while (l <= r){
        if (a[l] < p){
            l += 1
            continue
        }
        if (a[r] > p){
            r -= 1
            continue
        }
        var t = a[l]
        a[l] = a[r]
        a[r] = t
        l += 1
        r -= 1
    }
    quicksort_swift(&a, start, r + 1)
    quicksort_swift(&a, r + 1, end)
}

var a_swift:Int[] = [0,5,2,8,1234,-1,2]
let swift_start:UInt64 = mach_absolute_time();
println(swift_start)
quicksort_swift(&a_swift, 0, a_swift.count)
let swift_stop:UInt64 = mach_absolute_time();
println(swift_stop)
println(a_swift)

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swiftinsertRowsanddeleteRows注意事项发布时间:2022-07-13
下一篇:
[Swift]Codextended:JSON高效转Model发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap