學(xué)習(xí)到Kotlin的數(shù)組和集合,這里做個(gè)記錄瞻讽。
數(shù)組Array
Kotlin中數(shù)組也是個(gè)容器器腋,提供了不同的類型有:ByteArray
, CharArray
, ShortArray
, IntArray
, LongArray
, BooleanArray
, FloatArray
,DoubleArray
赤兴,一大堆队伟,這里不一一介紹,用法都差不多盆昙,只是帶類型的數(shù)組只能裝指定類型羽历。
初始化
//初始化一個(gè)空的數(shù)組,實(shí)際上它是大小為0繼承arrayOfNulls的數(shù)組
var array1 = emptyArray<String>()
//初始化一個(gè)空的數(shù)組淡喜,需要傳入數(shù)組大小
var array2 = arrayOfNulls<String>(3)
//初始化一個(gè)指定大小數(shù)組秕磷,第一個(gè)參數(shù)是size,第二個(gè)參數(shù)是生成邏輯的函數(shù)炼团,
// 相當(dāng)于java中遍歷數(shù)組澎嚣,給每個(gè)下標(biāo)為k的數(shù)組賦值array2[k] = k * k;
var array3 = Array(10) { k -> k * k}
//初始化一個(gè)多數(shù)據(jù)類型的數(shù)組
var array4 = arrayOf("1",2,3)
//初始化一個(gè)特定數(shù)據(jù)類型-字符類型的數(shù)組
var array5 = charArrayOf('d')
我們打印一下初始化后的數(shù)組,看看初始化的結(jié)果:
"/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java" -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=49730 "-Didea.launcher.bin.path=/Applications/Android Studio.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/Android Studio.app/Contents/lib/idea_rt.jar:/Applications/Android Studio.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/Android Studio.app/Contents/plugins/junit/lib/junit5-rt.jar:/Users/kingnewspring/Library/Android/sdk/platforms/android-27/data/res:/Users/kingnewspring/Downloads/KotlinTest/app/build/intermediates/classes/debug:/Users/kingnewspring/Downloads/KotlinTest/app/build/tmp/kotlin-classes/debug:/Users/kingnewspring/Downloads/KotlinTest/app/build/generated/res/rs/debug:/Users/kingnewspring/Downloads/KotlinTest/app/build/generated/res/resValues/debug:/Users/kingnewspring/Downloads/KotlinTest/app/build/tmp/kotlin-classes/debugUnitTest:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/android.arch.lifecycle/common/1.1.0/edf3f7bfb84a7521d0599efa3b0113a0ee90f85/common-1.1.0.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/com.android.support/support-annotations/27.1.1/39ded76b5e1ce1c5b2688e1d25cdc20ecee32007/support-annotations-27.1.1.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/animated-vector-drawable-27.1.1.aar/1be6be214d4bf5183a3327597856db9a/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-compat-27.1.1.aar/5318e651055d11149bff8ecc6ee849b7/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-compat-27.1.1.aar/5318e651055d11149bff8ecc6ee849b7/res:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.2.50/3811b225f9a22abf4f9d8a6f81adef0ba78a3c5/kotlin-stdlib-jdk7-1.2.50.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/viewmodel-1.1.0.aar/80fe4b8d90eafb67cf217b6d35b9d09e/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-vector-drawable-27.1.1.aar/c608acaab2dec891d8b39f2483c7e51b/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-core-ui-27.1.1.aar/18f9fb381d82d8ce4a1d4bda82e765cf/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-core-ui-27.1.1.aar/18f9fb381d82d8ce4a1d4bda82e765cf/res:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.2.50/6b19a2fcc29d34878b3aab33fd5fcf70458a73df/kotlin-stdlib-common-1.2.50.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-core-utils-27.1.1.aar/dc31deadc75ff47a72c687e48f9669d7/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.3.aar/0b9e57814d1dfbd5924ef90766162087/res:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.3.aar/0b9e57814d1dfbd5924ef90766162087/jars/classes.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.2.50/66d47b004c5b8a1d2d1df9e463187390ed741316/kotlin-stdlib-1.2.50.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/support-fragment-27.1.1.aar/52d6725357d2b27fdf8d861bfd464aa4/jars/classes.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/runtime-1.1.0.aar/6c59003f6d713b57600f30ff3d7250e3/jars/classes.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.1.1.aar/a88341bae42c2eb22d3a232380966a18/res:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/appcompat-v7-27.1.1.aar/a88341bae42c2eb22d3a232380966a18/jars/classes.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/livedata-core-1.1.0.aar/9683c5714c058ad2ce152c04a0e3a19d/jars/classes.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/android.arch.core/common/1.1.0/8007981f7d7540d89cd18471b8e5dcd2b4f99167/common-1.1.0.jar:/Users/kingnewspring/.gradle/caches/modules-2/files-2.1/com.android.support.constraint/constraint-layout-solver/1.1.3/bde0667d7414c16ed62d3cfe993cff7f9d732373/constraint-layout-solver-1.1.3.jar:/Users/kingnewspring/.gradle/caches/transforms-1/files-1.1/runtime-1.1.0.aar/e51aad556365d772d1b760355b0ea1f6/jars/classes.jar:/Users/kingnewspring/Downloads/KotlinTest/app/build/intermediates/sourceFolderJavaResources/test/debug:/Users/kingnewspring/Downloads/KotlinTest/app/build/intermediates/sourceFolderJavaResources/debug:/Users/kingnewspring/Downloads/KotlinTest/app/build/generated/mockable-android-27.v3.jar" com.intellij.rt.execution.application.AppMainV2 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.example.kingnewspring.kotlintest.ExampleUnitTest
arra2:null
arra2:null
arra2:null
arra3:0
arra3:1
arra3:4
arra3:9
arra3:16
arra3:25
arra3:36
arra3:49
arra3:64
arra3:81
arra4:1
arra4:2
arra4:3
arra5:d
Process finished with exit code 0
可以看到
-
array1
并沒(méi)有元素瘟芝,符合預(yù)期易桃,它是一個(gè)空數(shù)組,相當(dāng)于arrayOfNulls<String>(0)
-
array2
有3個(gè)元素锌俱,都是null -
array3
有10個(gè)元素晤郑,分別是下標(biāo)0~9的2次方,符合初始化邏輯 -
array4
有3個(gè)元素贸宏,和初始化的元素值一致 -
array5
有一個(gè)元素贩汉,就是char
類型的d
數(shù)組的元素?cái)U(kuò)充:plus
和Java
一樣,Kotlin
的array默認(rèn)大小就是你給定的锚赤,初始化是0就是0,初始化是100它就是100褐鸥,但是查看源碼线脚,你會(huì)發(fā)現(xiàn)kotlin還是提供了一個(gè)添加元素的api,plus
方法
/**
* Returns an array containing all elements of the original array and then the given [element].
*/
public actual operator fun <T> Array<T>.plus(element: T): Array<T> {
val index = size
val result = java.util.Arrays.copyOf(this, index + 1)
result[index] = element
return result
}
plus方法注釋上說(shuō),返回一個(gè)包含原來(lái)數(shù)組所有元素的數(shù)組浑侥,實(shí)際上就是將來(lái)原來(lái)的數(shù)組拷貝了一下,并且長(zhǎng)度+1,然后給數(shù)組添加新的元素姊舵,雖然性能上不好,但是可以很方便的添加元素寓落。這里注意的是括丁,方法返回的是一個(gè)新的數(shù)組,因此需要接收它伶选,否則添加是不成功的史飞,我們用array4
示范下:
//初始化一個(gè)多數(shù)據(jù)類型的數(shù)組
var array4 = arrayOf("1",2,3)
array4.plus("1")
for (i in array4){
println(i)
}
打印結(jié)果是:
1
2
3
Process finished with exit code 0
現(xiàn)在我們稍微改下代碼:
//初始化一個(gè)多數(shù)據(jù)類型的數(shù)組
var array4 = arrayOf("1",2,3)
//array4.plus("4")
array4 = array4.plus("4")
for (i in array4){
println(i)
}
此時(shí)的打印結(jié)果:
1
2
3
4
Process finished with exit code 0
數(shù)組元素的填充:fill
array提供了fill
函數(shù),給原有的數(shù)組填充數(shù)組(如果原來(lái)有數(shù)據(jù)將會(huì)被覆蓋)仰税,操作示范:
//初始化一個(gè)多數(shù)據(jù)類型的數(shù)組
var array4 = arrayOf("1",2,3)
//往數(shù)組里填入數(shù)據(jù)"4",fromIndex 0 toIndex 2,從下標(biāo)0到2,不包含2
array4.fill("4",0,2)
for (i in array4){
println(i)
}
結(jié)果可以猜得到:
4
4
3
Process finished with exit code 0
我們來(lái)看看fill
方法的源碼
/**
* Fills original array with the provided value.
*/
public fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
//這里調(diào)用了Java的fill操作
java.util.Arrays.fill(this, fromIndex, toIndex, element)
}
直接看上面的函數(shù)還看不出來(lái)原理构资,但是我們可以看到,他調(diào)用的是Java的fill方法陨簇,繼續(xù)查看java.util.Arrays.fill
/**
* Assigns the specified Object reference to each element of the specified
* range of the specified array of Objects. The range to be filled
* extends from index <tt>fromIndex</tt>, inclusive, to index
* <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
* range to be filled is empty.)
*
* @param a the array to be filled
* @param fromIndex the index of the first element (inclusive) to be
* filled with the specified value
* @param toIndex the index of the last element (exclusive) to be
* filled with the specified value
* @param val the value to be stored in all elements of the array
* @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
* @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
* <tt>toIndex > a.length</tt>
* @throws ArrayStoreException if the specified value is not of a
* runtime type that can be stored in the specified array
*/
public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}
很簡(jiǎn)單的操作吐绵,就是遍歷賦值
集合List
Kotlin集合和Java的集合類似,都實(shí)現(xiàn)了Collection
接口河绽,Kotlin集合分為
- 不可變集合(immutable)
- 可變集合(mutable)
初始化
//通過(guò)元素初始化一個(gè)不可變集合
var list1 = listOf("aaa", "bbb","ccc","dddd")
//初始化一個(gè)空的不可變集合
var list2 = emptyList<String>()
//初始化一個(gè)過(guò)濾空元素的不可變集合
var list3 = listOfNotNull(1,null)
//初始化一個(gè)空的不可變集合,和list2是一樣的
val list4:List<String> = listOf()
//創(chuàng)建一個(gè)帶元素的可變集合
var list5 = mutableListOf("22","33")
//創(chuàng)建一個(gè)空的arrayList己单,它也可變集合
var list6 = arrayListOf<String>()
這里不一一打印它們的元素了,需要注意的是
-
list3
耙饰,它只有一個(gè)元素是1纹笼,集合大小也是1,因?yàn)檫^(guò)濾調(diào)了null榔幸! -
list2
和list4
原理一樣允乐,我們看看list2
源碼
/**
* Returns an empty read-only list. The returned list is serializable (JVM).
* @sample samples.collections.Collections.Lists.emptyReadOnlyList
*/
public fun <T> emptyList(): List<T> = EmptyList
可以看到它是返回一個(gè)空的只讀list,并且在JVM
中是可序列化的(在android項(xiàng)目里Kotlin
現(xiàn)階段還是寄生在JVM
平臺(tái)的,所有的Kotlin
源代碼都會(huì)被編譯成class文件)
-
list5
和list6
都實(shí)現(xiàn)了MutableList
接口削咆,源碼可以看出來(lái)它們的等效的
/**
* Returns a new [MutableList] with the given elements.
* @sample samples.collections.Collections.Lists.mutableList
*/
public fun <T> mutableListOf(vararg elements: T): MutableList<T> =
if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
mutableListOf()
實(shí)際上也是調(diào)用ArrayList
集合元素的擴(kuò)充
不可變集合用plus
不可變集合牍疏,按理說(shuō)是不能擴(kuò)充元素的,因?yàn)樗麄冎蛔x(read—only)拨齐。但是你想要添加元素也行鳞陨,通過(guò)調(diào)用plus
方法,其原理和數(shù)組一樣瞻惋,都是創(chuàng)建新的集合厦滤,長(zhǎng)度+1,并不是真的可變,這里的plus
方法是個(gè)運(yùn)算符重載函數(shù),因此你也可以用"+"來(lái)添加
//通過(guò)元素初始化一個(gè)不可變集合
var list1 = listOf("aaa", "bbb","ccc","dddd")
//通過(guò)plus添加
list1 = list1.plus("fff")
//通過(guò)+添加
list1 += "eeee"
//打印
for (i in list1){
println(i)
}
結(jié)果是:
aaa
bbb
ccc
dddd
fff
eeee
Process finished with exit code 0
可變的集合用add
這個(gè)簡(jiǎn)單歼狼,直接add
進(jìn)來(lái)就是了掏导,和Java
的arraylist
使用一樣。有的人會(huì)問(wèn)了羽峰,這里能不能用plus
呢,答案是可以用趟咆,但是不能增加元素添瓷,和不可變集合一樣,我們來(lái)看看:
//創(chuàng)建一個(gè)帶元素的可變集合
var list5 = mutableListOf("22","33")
//使用plus添加
list5.plus("444")
//使用add添加
list5.add("r3r3")
//打印
for (i in list5){
println(i)
}
打印結(jié)果:
22
33
r3r3
Process finished with exit code 0
這里的plus
效果和不可變數(shù)組以及不可變集合里是一樣的值纱,非要使用需要對(duì)集合重新賦值鳞贷,參考不可變集合的使用,這里強(qiáng)烈不推薦使用
集合元素的填充
填充和Java
的List
也是極其相似,用法是一樣一樣的虐唠。無(wú)論可變不可變集合都一樣搀愧!
數(shù)組和集合的相互轉(zhuǎn)換
數(shù)組轉(zhuǎn)集合:toList()
val array1 = arrayOf(1,2,3)
//數(shù)組轉(zhuǎn)集合
var list7 = array1.toList()
println("list7 = $list7")
打印結(jié)果:
list7 = [1, 2, 3]
Process finished with exit code 0
集合轉(zhuǎn)數(shù)組:toTypedArray()
//通過(guò)元素初始化一個(gè)不可變集合
var list1 = listOf("aaa", "bbb","ccc","dddd")
//集合轉(zhuǎn)數(shù)組
val array1 = list1.toTypedArray()
for (temp in array1)
println(temp)
打印結(jié)果:
aaa
bbb
ccc
dddd
Process finished with exit code 0
總結(jié)
Kotlin
的array
和list
當(dāng)然還有一些別的方法,大家可以自己去看源碼或api疆偿,集合這里沒(méi)有講到map
和set
咱筛,有機(jī)會(huì)再補(bǔ)充