Swift- 數組

//: Playground - noun: a place where people can play編程

 

import UIKit設計模式

 

var str = "Hello, playground"數組

 

//容器類app

 

////1.數組 Array函數

//spa

////通常形式設計

//var numbers:[Int] = [0,1,2,3,4,5]索引

//var vowels:[String] = ["A","E","I","O","U"]rem

//it

////泛型形式

//var numbers1:Array<Int> = [0,1,2,3,4,5]

//var vowels1:Array<String> = ["A","E","I","O","U"]

//

////初始化空數組

//var emptyArray1:[Int] = []

//var emptyArray2:Array<Int> = []

//var emptyArray3 = [Int]()

//var emptyArray4 = Array<Int>()

//

////定義固定大小的數組並初始化爲0

//var allZeros = [Int](repeating: 0, count: 5)

//var allZeros2 = Array<Int>(repeating: 1, count: 5)

 

 

////數組的一些屬性

//var numbers = [1,2,3,4,5]

//var vowels = ["A","E","I","O","U"]

//var emptyArray = [Int]()

//

//numbers.isEmpty

//emptyArray.isEmpty

//

////數組索引

//vowels[2]

//vowels.count

//vowels.first

//vowels.last

//emptyArray.first

//emptyArray.last

//

//if let firstVowel = vowels.first {

//    print("The first vowel is" + firstVowel)

//}

//

//vowels.first!

//vowels[vowels.count - 1]

//

//numbers.min()

//numbers.max()

//

//numbers[2..<4]

//numbers[2..<numbers.count]

//vowels.contains("A")

//

//vowels.index(of: "E")

//vowels.index(of: "F")

//

////數組遍歷

//for number in numbers {

//    print(number)

//}

//

//for (i, vowel) in vowels.enumerated() {

//    print("\(i + 1): \(vowel)")

//}

//

//var oneToFive = [1,2,3,4,5]

//numbers == oneToFive//數組是有序的數據集合

 

//數組的操做

 

//1.

var courses = ["玩兒轉Swift","玩兒轉Swift第一季","玩兒轉Swift第二季"]

 

courses.append("玩兒轉Swift第三季")

print(courses)

 

courses += ["玩兒轉Swift第四季"]

courses = courses + ["玩兒轉Swift面向協議編程","玩兒轉Swift面向函數編程"]

 

print(courses)

 

 

courses.insert("玩兒轉Swift設計模式", at: 5)//5是索引值,從1開始

 

courses.remove(at: 4)

 

//

//courses.removeSubrange(0..<4)

//print(courses)

 

//

courses[0..<4] = ["玩兒轉Swift3"]

print(courses)

 

//二維數組

 

var board:Array<Array<Int>> = [[1024,16,2,0],

             [256,4,2,0],

             [64,2,0,0],

             [2,0,0,0]]

 

board[0]

board[0][0]

 

board.count

board[0].count

 

board[0].append(0)

board

board.append([0,0,0,0])

board += [[2,2,2,2]]//二維數組是添加一個元素(二維數組的元素仍是數組)

 

 

//NSArray

//var array1 = []

var array2 = [1,2,3,4,5] as NSArray

var array3:NSArray = [1,"Hello",3.0]

var array4:[NSObject] = [1 as NSObject,"Hello" as NSObject,3.0 as NSObject]

相關文章
相關標籤/搜索