/* * 集合類型-數組 * 1.有序可重複-Array索引從0開始 * 2.無序不重複-set * 3.無序可重複-Map,但值有惟一的鍵 * */ fun main(args: Array<String>) { //Array:Array<類型>或arrayof(元素1,元素2.元素3....元素n) //大小固定,元素類型不可變 // var stations= Array<String>("重慶","上海")
var stations1= arrayOf("重慶","上海","北京","上海")//經常使用
for (s in stations1) { println(s)//--->>重慶 上海 北京 上海
} //獲取篩選重複元素的數組:.distinct 或用.to Set轉換爲Set
val norepeat=stations1.distinct() val norepeat1=stations1.toSet() for (s in norepeat) { print(s)//--->>重慶上海北京
} //切割數組:sliceArray
val slice=stations1.slice(1..2)//下標--->>上海北京
for (slouse in slice) { print(slouse) } //建立一個有默認值的數組 Array(長度,{默認值})
var name=Array(20,{"默認值"}) for (s1 in name) { println(s1)//--->>默認值 默認值。。。。默認值
} //建立1-10數組:Array(10,i->i+1) //i表明元素的索引值從0開始
var a=Array(10,{i->i}) for (i in a){ println(i)//--->>0 1 2 3 4 5 6 7 8 9
} //元素計數:count(),空否:isEmpty()
println(a.count())//數組長度-->>10
println(a.isEmpty())//--->>false //獲取其中元素:數組名[索引],首元素:數組名.first,尾元素:數組名.last //獲取前5個元素的快捷方法.component1到5
println(a.first())//---->0
println("${a.component1()},${a.component5()}")//--->>0,4
println(a[5])//獲取第六個元素--->>5 //mutableList:MutableList<類型>或mutableListof(元素1.元素2,,,元素n) //大小可變,類型不可變
var stationsnew= mutableListOf("重慶","上海","北京","上海") var stationsnew1= arrayOf("涪陵","長壽") //在末尾增長:add()方法 // 添加另外一個數組addAll方法
stationsnew.add("廣州") stationsnew.addAll(stationsnew1) for (s in stationsnew) { print(s)//------>>重慶上海北京上海廣州涪陵長壽
} //移除元素remove,移出指定位置removeAt
stationsnew.removeAt(0) stationsnew.removeAll(stationsnew1) }