學習參考位址
1、建立及初始化陣列
var shoppinglist = ["water","book","food"]
println(shoppinglist[1])
shoppinglist[2] = "food from china"
println(shoppinglist)
//這是最初級的建立陣列,並且通過下標key訪問陣列內容及變更陣列內容。
錯誤展示:
var shoppinglist = ["water","book","food"]
//consecutive statements on a line must be separated by ';'
改進後:
var shoppinglist: = ["water","book","food"]
//type annotation missing in pattern
再改進:
var shoppinglist:string = ["water","book","food"]
//type annotation missing in pattern
得到最終標準錄入:
var shoppinglist:[string] = ["water","book","food"]
2、陣列型別
用四種方式建立陣列型別
var array = ["hello",1,23.3]//right
var array1:[int] = ["hello",1,23.3]//error定義的是int不能含有字串
var array1:[int] = [1,2,3,4,5]//right
var array2:array = [1,2,3,4,5]//宣告了陣列型別
var array3:array = [1,2,3,4,5]//把裡面的int換成float可以,但是不能換成string。經過測試即使陣列元素本身就是字串也不行。
//需要注意:不能寫成var array3:。只能用[int]
3、獲取陣列的數量
var shoppinglist:[string] = ["water","book","food"]
var a = shoppinglist.count
println(a)
4、檢查陣列是否為空,使用isempty,isempty的真返回的是0.
var shoppinglist:[string] = ["water","book","food"]
var a = shoppinglist.count
if shoppinglist.isempty
else
5、向陣列中新增新元素
var shoppinglist:[string] = ["water","book","food"]
println(shoppinglist)
另一種方式:
//定義了陣列型別
var shoppinglist:[string] = ["water","book","food"]
shoppinglist += ["banana","*****"]
println(shoppinglist)
//沒有定義陣列型別
var array = [1,"hello",34.09]
array += ["world",90,99.3]
println(array)
6、修改陣列中的資料
//定義了陣列型別
var shoppinglist:[string] = ["water","book","food"]
shoppinglist += ["banana","*****"]
println(shoppinglist)
shoppinglist[0] = "rose"
println(shoppinglist)
//沒有定義陣列型別
var array = [1,"hello",34.09]
array += ["world",90,99.3]
println(array)
array[0] = 0
println(array)
修改相連陣列元素的方式:
var shoppinglist:[string] = ["water","book","food"]
shoppinglist += ["banana","*****"]
println(shoppinglist)
//0-2是連續的,並且不能寫成[0...1,3...4]這樣寫是無效的
shoppinglist[0...2] = ["h","o","n"]
println(shoppinglist)
7、向陣列內插入元素
ar shoppinglist:[string] = ["water","book","food"]
//只能再陣列後面新增元素
shoppinglist += ["banana","*****"]
println(shoppinglist)
//可以將想插入的元素放到陣列的任意位置。
shoppinglist.insert("rose", atindex: 1)
println(shoppinglist)
8、移除陣列元素
var shoppinglist:[string] = ["water","book","food"]
//只能再陣列後面新增元素
shoppinglist += ["banana","*****"]
println(shoppinglist)
//移除第乙個元素water
shoppinglist.removeatindex(0)
println(shoppinglist)
//移除末位元素
shoppinglist.removelast()
println(shoppinglist)
//移除第乙個到第二個元素
shoppinglist.removerange(0...1)
println(shoppinglist)
9、遍歷陣列元素
var shoppinglist:[string] = ["water","book","food"]
//只能再陣列後面新增元素
shoppinglist += ["banana","*****"]
for i in shoppinglist
用for迴圈給空陣列中新增元素
var array:[string]=
for index in 1...10
10、用列舉的方式遍歷陣列中的元素
var shoppinglist:[string] = ["water","book","food"]
//只能再陣列後面新增元素
shoppinglist += ["banana","*****"]
for (i,value) in enumerate(shoppinglist)
再for迴圈中(i,value),value指的是陣列中的元素和i相對應的值,並且括號中只能是兩個值,如果再增添乙個就出錯了例如:(i,value,b)
Python之路 數字
python 數字資料型別用於儲存數值。資料型別是不允許改變的,這就意味著如果改變量字資料型別的值,將重新分配記憶體空間。以下例項在變數賦值時 number 物件將被建立 var1 1 var2 10您也可以使用del語句刪除一些數字物件的引用。del語句的語法是 del var1 var2 var...
LinuxC語言學習之路 陣列定義,輸入,輸出。
相同型別的變數集合就是乙個陣列 切記 陣列實際上就是變數 int a a 5 陣列必須要先定義之後再使用 如何來定義陣列 格式 型別識別符號 陣列名 長度 比如 int a 4 這句話的意義是定義了乙個名字為a的陣列,他是乙個整型陣列,而且它裡面含有四個元素。每乙個元素都是乙個整型變數。他們分別是a...
演算法之路 陣列篇(02)存在重複元素
給定乙個整數陣列,判斷是否存在重複元素。如果任意一值在陣列 現至少兩次,函式返回true。如果陣列中每個元素都不相同,則返回false。輸入 1,2,3,1 輸出 true輸入 1,2,3,4 輸出 false三種解法,遍歷的方法這裡不提及 1排序看是否有相鄰元素 2陣列內容做輔助陣列下標 這是我在...