Swift- 数组

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

 

import UIKit

 

var str = "Hello, playground"

 

//容器类

 

////1.数组 Array

//

////一般形式

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

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

//

////泛型形式

//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]

 

 

 

 

 

 

 

 

 

posted @ 2016-11-03 15:32  一枚IT女  阅读(512)  评论(0编辑  收藏  举报