首页 > 代码库 > Swift学习笔记(5)--数组

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

 

数组的下标从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 = cityArraycityArray0[0...2] = ["Beijing","Shanghai","Guangzhou"]println(cityArray0) //[Beijing, Shanghai, Guangzhou, Seattle, NewYork]    //2.2 下标多于元素时,原数组中多出的下标数据被remove出原数组,即多退var cityArray2 = cityArraycityArray2[0...3] = ["Beijing","Shanghai","Guangzhou"]println(cityArray2) //[Beijing, Shanghai, Guangzhou, NewYork]    //2.3 下标少于元素时,将多出的元素添加到原数组,即少补var cityArray3 = cityArraycityArray3[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)