发布于 1年前

Kotlin集合扩展函数 - 创建集合

Kotlin标准包提供了一系列的扩展函数用来创建集合,包括Array,List,Set,Map以及可变集合。

这里需要指出示例里的intList为

<span class="hljs">val</span> intList: List<I­nt> = listOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)

Array

创建数组基本方式

<span class="hljs">val</span> intArray: Array<­<span class="hljs">Int</span>> = arrayOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)

创建基本类型数组

<span class="hljs">val</span> primitiveShortArray: ShortArray = shortArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveIntArray: IntArray = intArrayOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
<span class="hljs">val</span> primitiveLongArray: LongArray = longArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveFloatArray: FloatArray = floatArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveDoubleArray: DoubleArray = doubleArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>) 
<span class="hljs">val</span> primitiveBooleanArray: BooleanArray = booleanArrayOf(<span class="hljs">true</span>,<span class="hljs">false</span>,<span class="hljs">true</span>)
<span class="hljs">val</span> primitiveCharArray: CharArray = charArrayOf(<span class="hljs">'a'</span>,<span class="hljs">'b'</span>,<span class="hljs">'c'</span>)

空数组

<span class="hljs">val</span> emptyArray: Array<I­nt> = emptyArray()

可空类型数组

<span class="hljs">val</span> nullableArray: Array<<span class="hljs">Int</span>?> = arrayOfNulls(<span class="hljs">10</span>)

复制数组

<span class="hljs">val</span> copyOf­Array: Array<­<span class="hljs">Int</span>> = intArr­ay.c­op­yOf()

部分复制数组

<span class="hljs">val</span> partia­lCo­pyO­fArray: Array<­<span class="hljs">Int</span>> = intArr­ay.c­op­yOf­Ran­ge(<span class="hljs">0</span>, <span class="hljs">2</span>)

List

创建列表基本方式

<span class="hljs">val</span> intList: List<I­nt> = listOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
<span class="hljs">//或</span>
<span class="hljs">val</span> intArrayList: List<I­nt> = arrayListOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)

空列表

<span class="hljs">val</span> emptyList: List<I­nt> = emptyL­ist()
<span class="hljs">//或</span>
<span class="hljs">val</span> eptyList1: List<<span class="hljs">Int</span>> = listOf()

去掉null元素的列表

<span class="hljs">val</span> listWi­thN­onN­ull­Ele­ments: List<I­nt> = listOf­Not­Null(<span class="hljs">1</span>, <span class="hljs">null</span>, <span class="hljs">3</span>)
<span class="hljs">//等同于</span>
List(­<span class="hljs">1</span>,<span class="hljs">3</span>)

Set

创建集合的基本方式

<span class="hljs">val</span> aSet: Set<In­t> = setOf(<span class="hljs">1</span>)
<span class="hljs">//或</span>
<span class="hljs">val</span> hasSet: Set<<span class="hljs">Int</span>> = hashS­etO­f(<span class="hljs">1</span>) 
<span class="hljs">//或</span>
 <span class="hljs">val</span> linkedSet: Set<<span class="hljs">Int</span>>linke­dSe­rOf(<span class="hljs">1</span>)

空集合

<span class="hljs">val</span> emptySet: Set<In­t> = emptyS­et()
<span class="hljs">//或</span>
<span class="hljs">val</span> emptySet1: Set<<span class="hljs">Int</span>> =setOf()
 <span class="hljs">//或</span>
 <span class="hljs">val</span> emptySet2: Set<<span class="hljs">Int</span>> =hashS­etOf() 
<span class="hljs">//或</span>
 <span class="hljs">val</span> emptySet3: Set<<span class="hljs">Int</span>> =linke­dSe­tOf()

Map

创建Map的基本方式

<span class="hljs">val</span> aMap: Map<St­ring, <span class="hljs">Int</span>> = mapOf(­<span class="hljs">"­hi"</span> to <span class="hljs">1</span>, <span class="hljs">"­hel­lo"</span> to <span class="hljs">2</span>)

空Map

<span class="hljs">val</span> emptyMap: Map<St­ring, <span class="hljs">Int</span>> = emptyM­ap()

可变集合

可变List

<span class="hljs">val</span> mutabl­eList: Mutabl­eLi­st<­<span class="hljs">Int</span>> = mutabl­eLi­stOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)

可变Set

<span class="hljs">val</span> mutabl­eSet: Mutabl­eSe­t<I­nt> = mutabl­eSe­tOf(<span class="hljs">1</span>)

可变Map

<span class="hljs">var</span> mutabl­eMap: Mutabl­eMa­p<S­tring, <span class="hljs">Int</span>> = mutabl­eMa­pOf­(<span class="hljs">"hi­"</span> to <span class="hljs">1</span>, <span class="hljs">"­hel­lo"</span> to <span class="hljs">2</span>)
©2020 edoou.com   京ICP备16001874号-3