基本操作
var numbers: Array<Int> = [1,2,3,4,5]
var vowels: Array<String> = ["A","S","F","G"]
var emptyArray1:[Int]=[]
var emptyArray2:Array<Int> = []
var emptyArray3 = [Int]()
var emptyArray4 = Array<Int>()
var allZeros = [Int](count: 5, repeatedValue: 0)
var allZeros2 = Array<Int>(count: 5, repeatedValue: 0)
vowels.count
numbers.isEmpty
vowels[0]
vowels.first//可选型,不一定存在
vowels.last
//最大/小的元素
numbers.minElement()//调用的是一个方法
vowels.maxElement()
//子数组
numbers[2..<4]
numbers[2..<numbers.count]
//是否包含某个元素
vowels.contains("A")
vowels.contains("E")
//验证一个元素是否在数组中,返回元素的位置
vowels.indexOf("A")
vowels.indexOf("E")
for index in 0..<numbers.count{
numbers[index]
}
for number in numbers{
print(number)
}
for (i,vowel) in vowels.enumerate(){
print("\(i+1); \(vowel)")
}
var oneToFive = [1,2,3,4,5]
numbers == oneToFive//true 数组是有序的数据集合,元素位置变了,就不等了
数组的增删改查
var courses = ["one","two","three"]
courses.append("four")
courses += ["five"]
courses.insert("four2", atIndex: 4)//注意数组越界
courses.removeFirst()
courses.removeLast()
courses.removeAtIndex(2)
//courses.removeRange(0..<3)
//courses.removeAll()
courses[0] = "six"
print(courses)
courses[1...3] = ["seven","eight","nine"]
二维数组
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.append([0,0,0,0])
board += [[1,1,1,1]]
//UI---UserInterface用户界面---UIKit
//NS---NextStep----NSObject
//NSarray
var array1 = []
var array2 = [1,2,3,4,5] as NSArray
var array3:NSArray = [1,"hello",3.0]
var array4 = [1,"hello",3.0]//array4是NSObject类型---一般不这样写--改为
var array5:NSObject = [1,"hello",3.0]