一、說明
swift陣列中的型別必須一致,這一點與oc不同
// 陣列初始化
var numbers = [0,1,2,3,4,5]
var vowels = ["a","e","i","o","u"]
// 陣列的型別: [int] 或者 array
//var numbers: [int] = [0,1,2,3,4,5]
//var numbers: array = [0,1,2,3,4,5]
// 空陣列
var emptyarray1:[int] =
var emptyarray2:array =
var emptyarray3 = [int]()
var emptyarray4 = array()
// 建立具有預設值的陣列(相同元素的陣列)
var allzeros = array(repeating: 0, count: 5)
//[0,0,0,0,0]
var allzeros2 = [int](repeating: 0, count: 5)
//[0,0,0,0,0]
二、常用方法
var numbers = [1,2,3,4,5]
var vowels = ["a","e","i","o","u"]
var emptyarray = [int]()
// 陣列長度
vowels.count
// 判空
numbers.isempty
emptyarray.isempty
// 獲取元素
vowels[2]
// 陣列越界是乙個嚴重的錯誤
//vowels[-1]
//vowels[5]
// 獲取第乙個元素和最後乙個元素,返回的是可選型
vowels.first
vowels.last //.first和.last的返回值都為可選型
emptyarray.first
if let firstvowel = vowels.first
vowels.first!
vowels[vowels.count-1]
// 獲取最小,最大值
numbers.min() //1
vowels.max() //u
// 使用範圍
nupwvcdnmbers[2..<4] //[3,4]
numbers[2..
三、更多操作
var courses = ["a","b","c"]
// 新增元素
courses.append("d") //["a","b","c","d"]
print(程式設計客棧courses)
// 陣列常量
//使用let定義的陣列不可以更改任何內容
courses += ["e"] //+=後面必須和前面的型別一致 //["a","b","c","d","e"]
print(courses)
// 兩個陣列相加
courses = courses + ["f","g"] //+後面必須是陣列
//["a","b","c","d","e","f","g"]
print(courses)
courses.insert("q", at: 5)
//["a", "b", 程式設計客棧"c", "d", "e", "q", "f", "g"]
print(courses)
// 刪除元素
courses.removelast()
//["a", "b", "c", "d", "e", "q", "f"]
print(courses)
courses.removefirst()
//["b", "c", "d", "e", "q", "f"]
print(courses)
courses.remove(at: 4)
//["b", "c", "d", "e", "f"]
print(courses)
//區間刪除操作
<4)
<10)
//print(courses)
//print(courses)
// 修改元程式設計客棧素
courses[0] = "w"
//["w", "c", "d", "e", "f"]
print(courses)
//範圍修改
courses[1...3] = ["w","w","w"]
//["w", "w", "w", "w", "f"]
print(courses)
courses[0...3] = ["w"]
//["w", "f"]
print(courses)
四、二維陣列
var board = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:[[int]] = [ [1024,16,2,0] , [256,4,2,0] 程式設計客棧, [64,2,0,0] , [2,0,0,0] ]
//var board:[array] = = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:array = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:array> = [ [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
// swift中的二維陣列,每一維度的元素數目可以不同
board[0].append(0)
board
// 為二維陣列的第乙個維度新增的元素是乙個陣列
board.append([0,0,0,0])
board += [ [0,0,0,0] ]
board
五、nsarray
nsarray是乙個類,array是乙個結構體
var array1 = //會預設是nsarray,swift3.0之後該寫法廢除
var array2 = [1,2,3,4,5] as nsarray
var array3: nsarray = [1,"hello"]
var array4: [nsobject] = [1 as nsobject,"hello" as nsobject]
本文標題: swift陣列詳細用法解析
本文位址:
select函式詳細用法解析
1 表頭檔案 include include include 2 函式原型 int select int n,fd set readfds,fd set writefds,fd set exceptfds,struct timeval timeout 3 函式說明 select 用來等待檔案描述詞狀...
select函式詳細用法解析
1 表頭檔案 include include include 2 函式原型 int select int n,fd set readfds,fd set writefds,fd set exceptfds,struct timeval timeout 3 函式說明 select 用來等待檔案描述詞狀...
c str 的用法詳細解析
const char c str c str 函式返回乙個指向正規c字串的指標,內容與本string串相同.c str 就是把string類物件轉換成和c相容的char 型別。這是為了與c語言相容,在c語言中沒有string型別,故必須通過string類物件的成員函式c str 把string 物件...