1.區(qū)間(Range)
- 一個數(shù)學上的概念乔妈,表示范圍
- ClosedRange的子類,IntRange最常用
- 基本寫法:
- 0..100表示[0,100]
- 0 until 100 表示[0和橙,100)
- i in 0..100 判斷i是否在區(qū)間[0,100]中
val range: IntRange = 0..1024 //[0,1024]---閉區(qū)間
val range_exclusive: IntRange = 0 until 30 //[0,30)---半閉區(qū)間
val emptyRange: IntRange = 0..-1
fun main(args: Array<String>) {
println(emptyRange.isEmpty())
println(range.contains(100))
println(50 in range)
for (i in range_exclusive) {
print("$i ")
}
}
true
true
true
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
2.數(shù)組類型
基本寫法:
- val array:Array<String> = arrayOf(...)
基本類型的數(shù)組
為了避免不必要的裝箱費和拆箱,基本類型的數(shù)組是定制的
Java | Kotlin |
---|---|
int[] | IntArray |
short[] | ShortArray |
long[] | LongArray |
float[] | FloatArray |
double[] | DoubleArray |
char[] | CharArray |
基本操作
- print array[i] 輸出第i個元素
- array[i] = "Hello" 給第i個成員賦值
- array.length 數(shù)組的長度
val arrayOfInt: IntArray = intArrayOf(1, 2, 3, 4, 5, 6, 7)
val arrayOfChar: CharArray = charArrayOf('H', 'e', 'l', 'l', 'o', ',', 'W', 'o', 'r', 'l', 'd', '!')
val arrayOfString:Array<String> = arrayOf("你好","美女")
fun main(args: Array<String>) {
println(arrayOfInt.size)
for (i in arrayOfInt){
println(i)
}
println(arrayOfInt.slice(2..4))
println(arrayOfChar.joinToString())
println(arrayOfChar.joinToString(""))
println(arrayOfString[0])
arrayOfString[1] ="帥哥"
println(arrayOfString[1])
}
1
2
3
4
5
6
7
[3, 4, 5]
H, e, l, l, o, ,, W, o, r, l, d, !
Hello,World!
你好
/**
*集合類型
*
* 同類型的值的組合裹匙,根據(jù)整體特性分:
* 1.有序可重復 - Array 儿惫,索引從0開始(index,i)
* 2.無序不重復 - set
* 3.無序可重復 - Map ,有唯一的鍵(key)
* */
fun main(args: Array<String>) {
//Array: Array<類型> 或arrayOf(元素一、元素二列荔、元素三卸例,...元素三)
var country = arrayOf("China", "Japan", "American", "Germany", "China", "China")
for (s in country) {
println(s)
}
//創(chuàng)建一個有默認值的數(shù)組,Array(計算,{默認值})
println("----------------創(chuàng)建一個有默認值的數(shù)組----------------")
var countryPlaceHolder = Array(5, { "Australian" })
for (s in countryPlaceHolder) {
println(s)
}
//創(chuàng)建1到10的數(shù)組:Array(10,{i -> i+1})
//i代表元素的索引值肌毅,從0開始
println("----------------創(chuàng)建1到10的Array數(shù)組----------------")
var oneToTen = Array(10, { i -> i + 1 })
for (i in oneToTen) {
println(i)
}
//元素計數(shù):count,空否:isEmpty()
println("元素計數(shù):" + oneToTen.count())
println("元素是否為空:" + oneToTen.isEmpty())
//獲取其中元素:數(shù)組名{索引}姑原,首元素:數(shù)組名.frist悬而,尾元素:數(shù)組名.last
//獲取前5各元素的快捷方法.compoent 1到5
println("${oneToTen.component1()},${oneToTen.component2()}")
println("獲取第三個元素" + oneToTen[2])
println("獲取最后一個元素" + oneToTen.last())
//獲取篩選重復后的數(shù)組:.distinct() 或用.toSet()轉(zhuǎn)換為set。
println("----------------獲取篩選重復后的數(shù)組----------------")
val countryNotRepeat = country.distinct()
for (i in countryNotRepeat) {
println(i)
}
println("----------------獲取篩選重復后的set----------------")
val countryNotRepeat2 = country.toSet()
for (i in countryNotRepeat2) {
println(i)
}
//切割數(shù)組:sliceArray
println("----------------切割數(shù)組:sliceArray----------------")
val countrySlice = country.sliceArray(2..3)
for (s in countrySlice) {
println(s)
}
//mutableList:MutableList<類型>或mutableListOf(元素1锭汛,元素2笨奠,...,元素n)
//大小可變唤殴,類型不可變
println("----------------新建mutableList----------------")
var mutableCountry = mutableListOf("China", "Japan", "American", "Germany", "China", "China")
for (s in mutableCountry) {
println(s)
}
println("----------------移除首位置元素----------------")
mutableCountry.removeAt(0)
for (s in mutableCountry) {
println(s)
}
//在末尾增加元素:add元素般婆,添加另一個數(shù)組addAll方法
println("----------------add添加元素----------------")
mutableCountry.add("Korea")
for (s in mutableCountry) {
println(s)
}
val newCountry = arrayOf("Britain", "France")
println("----------------整體添加元素----------------")
mutableCountry.addAll(newCountry)
for (s in mutableCountry) {
println(s)
}
println("----------------移除新加的數(shù)組元素----------------")
mutableCountry.removeAll(newCountry)
for (s in mutableCountry) {
println(s)
}
println("----------------獲取篩選重復后的mutableList----------------")
for (s in mutableCountry.distinct()) {
println(s)
}
}
China
Japan
American
Germany
China
China
----------------創(chuàng)建一個有默認值的數(shù)組----------------
Australian
Australian
Australian
Australian
Australian
----------------創(chuàng)建1到10的Array數(shù)組----------------
1
2
3
4
5
6
7
8
9
10
元素計數(shù):10
元素是否為空:false
1,2
獲取第三個元素3
獲取最后一個元素10
----------------獲取篩選重復后的數(shù)組----------------
China
Japan
American
Germany
----------------獲取篩選重復后的set----------------
China
Japan
American
Germany
----------------切割數(shù)組:sliceArray----------------
American
Germany
----------------新建mutableList----------------
China
Japan
American
Germany
China
China
----------------移除首位置元素----------------
Japan
American
Germany
China
China
----------------add添加元素----------------
Japan
American
Germany
China
China
Korea
----------------整體添加元素----------------
Japan
American
Germany
China
China
Korea
Britain
France
----------------移除新加的數(shù)組元素----------------
Japan
American
Germany
China
China
Korea
----------------獲取篩選重復后的mutableList----------------
Japan
American
Germany
China
Korea
3.Set集合
/**
* 集合類型 Set:無序不重復
* 主要方法 交差并補
* */
fun main(args: Array<String>) {
//Set<類型> 或 setOf(元素1,元素2朵逝,...蔚袍,元素n)
//大小不固定,元素類型不可變
val country = setOf("Britain", "France", "China", "Japan", "American", "Germany")
val countryAsia = setOf("China", "Japan", "Korea", "Vietnam")
val countryPermanent = setOf("American", "China", "France", "Britain", "Russia")
for (s in country) {
println(s)
}
//元素計數(shù):count()配名,空否:isEmpty()
println("元素計數(shù):" + country.count())
//檢查是否包含某個元素:contains啤咽,包含另一個set:containAll
println("否包含China元素:" + country.contains("China"))
//轉(zhuǎn)換成數(shù)組:toTypedArray()
println("----------------轉(zhuǎn)換成數(shù)組:toTypedArray()----------------")
val array = country.toTypedArray()
for (s in array) {
println(s)
}
//結(jié)合之間的運算:intersect/subtract/union/minus(補集的被操作對象無需是Set類型)
println("----------------集合交集數(shù)據(jù):intersect----------------")
val interCountry = country.intersect(countryAsia)
for (s in interCountry) {
println(s)
}
println("----------------集合差集數(shù)據(jù):subtract----------------")
val subtractCountry = country.subtract(countryAsia)
for (s in subtractCountry) {
println(s)
}
println("----------------集合并集數(shù)據(jù):union----------------")
val unionCountry = country.union(countryAsia).union(countryPermanent)
for (s in unionCountry) {
println(s)
}
println("----------------集合補集數(shù)據(jù):minus----------------")
val minusCountry = country.minus(countryAsia)
for (s in minusCountry) {
println(s)
}
//MutableSet<類型>或mutableSetOf(元素1,元素2渠脉,...宇整,元素n)
//大小可變,類型不可變
val mutableCountry = country.union(countryAsia).toMutableSet()
println("----------------MutableSet----------------")
for (s in mutableCountry) {
println(s)
}
println("----------------MutableSet添加數(shù)據(jù):add----------------")
mutableCountry.add("Thailand")
for (s in mutableCountry) {
println(s)
}
println("----------------MutableSet添加集合數(shù)據(jù):addAll----------------")
val newCountry = setOf("Brazil","Mexico")
mutableCountry.addAll(newCountry)
for (s in mutableCountry) {
println(s)
}
println("----------------MutableSet移除數(shù)據(jù):remove----------------")
//移除元素:remove芋膘,移除另一集合:removeAll
mutableCountry.remove("Thailand")
for (s in mutableCountry) {
println(s)
}
println("----------------MutableSet移除集合數(shù)據(jù):removeAll----------------")
//移除集合元素:removeAll鳞青,移除另一集合:removeAll
mutableCountry.removeAll(newCountry)
for (s in mutableCountry) {
println(s)
}
}
Britain
France
China
Japan
American
Germany
元素計數(shù):6
否包含China元素:true
----------------轉(zhuǎn)換成數(shù)組:toTypedArray()----------------
Britain
France
China
Japan
American
Germany
----------------集合交集數(shù)據(jù):intersect----------------
China
Japan
----------------集合差集數(shù)據(jù):subtract----------------
Britain
France
American
Germany
----------------集合并集數(shù)據(jù):union----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Russia
----------------集合補集數(shù)據(jù):minus----------------
Britain
France
American
Germany
----------------MutableSet----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
----------------MutableSet添加數(shù)據(jù):add----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Thailand
----------------MutableSet添加集合數(shù)據(jù):addAll----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Thailand
Brazil
Mexico
----------------MutableSet移除數(shù)據(jù):remove----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Brazil
Mexico
----------------MutableSet移除集合數(shù)據(jù):removeAll----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
4.Map集合
/**
* 集合類型 Map :無序可重復,類似于"字典"的意思
*
* */
fun main(args: Array<String>) {
//mapOf<Key,Value>(Pair(key,value)为朋,...)
//顯示指定類型臂拓,可防止初始化值類型的錯誤
val products = mapOf<String, String>(Pair("IPhone", "6888"), Pair("Xiaomi", "3499"), Pair("Huawei", "5999"))
//元素計數(shù):size,空否:isEmpty
println("元素計數(shù):" + products.size)
//獲取某個key對應的value:get潜腻,getOrDefalt
println("獲取某個key對應的value:" + products.get("IPhone"))
println("獲取某個key對應的value埃儿,給定默認值:" + products.getOrDefault("ZTE", "0"))
println("----------------獲得map所有的key:keys----------------")
//返回所有的key:keys,所有的value:values
for (key in products.keys) {
println(key)
}
println("----------------獲得map所有的value:values----------------")
for (value in products.values) {
println(value)
}
//mutableMapOf<Key,Value>(Pair(key,value),...)
val mutableProducts = products.toMutableMap()
println("----------------獲得map所有的value:values----------------")
//添加或更新:下標方法 map變量名[key] = value
mutableProducts["IPhone"]="7999"
mutableProducts["Xiaomi"]="1999"
for (mutableProduct in mutableProducts) {
println("${mutableProduct.key}融涣,"+"${mutableProduct.value}")
}
println("----------------移除元素:remove----------------")
//移除元素:remove
mutableProducts.remove("IPhone")
for (mutableProduct in mutableProducts) {
println("${mutableProduct.key}童番,"+"${mutableProduct.value}")
}
println("----------------添加元素:put----------------")
//添加元素:remove
mutableProducts.put("Samsung","6999")
for (mutableProduct in mutableProducts) {
println("${mutableProduct.key}精钮,"+"${mutableProduct.value}")
}
}
元素計數(shù):3
獲取某個key對應的value:6888
獲取某個key對應的value,給定默認值:0
----------------獲得map所有的key:keys----------------
IPhone
Xiaomi
Huawei
----------------獲得map所有的value:values----------------
6888
3499
5999
----------------獲得map所有的value:values----------------
IPhone剃斧,7999
Xiaomi轨香,1999
Huawei,5999
----------------移除元素:remove----------------
Xiaomi幼东,1999
Huawei臂容,5999
----------------添加元素:put----------------
Xiaomi,1999
Huawei根蟹,5999
Samsung脓杉,6999