Kotlin Note Data Container (9)

Kotlin Note Data Container (9)

Kotlin note data type (1) Kotlin note string (2) Kotlin note operator (3) Kotlin note function (4) Kotlin note object-oriented (5) Kotlin note inheritance, abstract class, interface (6) Kotlin note high-order function (7) Kotlin Notes Generics (8)








1. Array

1. Object array usage

fun main(args: Array<String>) {
    
    
// 静态初始化
val intArray1 = arrayOf(21, 32, 43, 45) ①
val strArray1 = arrayOf("张三", "李四", "王五", "董六")// 动态初始化
val strArray2 = arrayOfNulls<String>(4)// 初始化数组中元素
strArray2[0] = "张三"
strArray2[1] = "李四"
strArray2[2] = "王五"
strArray2[3] = "董六"
val intArray2 = Array<Int>(10) {
    
     i -> i * i }//可以使用{ it * it }替代 ④
val intArray3 = Array<Int?>(10) {
    
     it * it * it } //可以使用{ i -> i * i * i }替代 ⑤
//遍历集合
for (item in intArray2) {
    
    println(item)
}
for (idx in strArray1.indices) {
    
    println(strArray1[idx])
}
}

2. Array of basic data types

fun main(args: Array<String>) {
    
    
// 静态初始化
val array1 = shortArrayOf(20, 10, 50, 40, 30)// 动态初始化
val array2 = CharArray(3) ②
array2[0] = 'C'
array2[1] = 'B'
array2[2] = 'D'
// 动态初始化
val array3 = IntArray(10) {
    
     it * it }299
//遍历集合
for (item in array3) {
    
    println(item)
}
println()
for (idx in array2.indices) {
    
    println(array2[idx])
}
}

Two, collection

1.set collection

A Set collection is a collection of unordered, non-repeatable elements of the same type

2. Immutable Set collection

fun main(args: Array<String>) {
    
    
val set1 = setOf("ABC") //[ABC] 
val set2 = setOf<Long?>() //[] 
val set3 = setOf(1, 3, 34, 54, 75)//[1, 3, 34, 54, 75] 
println(set1.size) //1 
println(set2.isEmpty()) //true 
println(set3.contains(75)) //true 
// 1.使用for循环遍历
println("--1.使用for循环遍历--")
for (item in set3) {
    
    println("读取集合元素: $item")
}
// 2.使用迭代器遍历
println("--2.使用迭代器遍历--")
val it = set3.iterator() 
while (it.hasNext()) {
    
     
val item = it.next() 
println("读取集合元素: $item")
}
}

3. Variable Set collection

fun main(args: Array<String>) {
    
    
val set1 = mutableSetOf(1, 3, 34, 54, 75) ①
val set2 = mutableSetOf<String>() ②
val set3 = hashSetOf<Long?>() ③
val set4 = hashSetOf("B", "D", "F") ④
val b = "B"
// 向set2集合中添加元素
set2.add("A")
set2.add(b) ⑤
set2.add("C")
set2.add(b) ⑥
set2.add("D")
set2.add("E")
// 打印集合元素个数
println("集合size = ${set2.size}") //5 ⑦
// 打印集合
println(set2)
// 删除集合中第一个"B"元素
set2.remove(b)
// 判断集合中是否包含"B"元素
println("""是否包含"B":${set2.contains(b)}""") //false
// 判断集合是否为空
println("set集合是空的:${set2.isEmpty()}") //false
// 清空集合
set2.clear()
println(set2.isEmpty()) //true
// 向set3集合中添加元素
set3.add(3)
set3.add(4)
set3.add(6)
// 1.使用for循环遍历
println("--1.使用for循环遍历--")
for (item in set2) {
    
    
println("读取集合元素: $item")
}
// 2.使用迭代器遍历
println("--2.使用迭代器遍历--")
val it = set3.iterator()
while (it.hasNext()) {
    
    
val item = it.next()
println("读取集合元素: $item")
}
}

2. list collection

The elements in the List collection are ordered and can appear repeatedly

1. Immutable list

fun main(args: Array<String>) {
    
    
val list1 = listOf("ABC") //[ABC] ①
val list2 = listOf<Long?>() //[] ②
val list3 = listOf(3, 34, 54, 75)//[3, 75, 54, 75] ③
val list4 = list3.subList(1, 3)//[75, 54] ④
println(list1.size) //1
println(list2.isEmpty()) //true
println(list3.contains(54)) //true
println(list3.indexOf(75)) //1 ⑤
println(list3.lastIndexOf(75)) //3 ⑥
//通过下标访问
println(list3[1]) //75 ⑦
// 1.使用for循环遍历
println("--1.使用for循环遍历--")
for (item in list3) {
    
    
println("读取集合元素: $item")
}
// 2.使用迭代器遍历
println("--2.使用迭代器遍历--")
val it = list3.iterator()
while (it.hasNext()) {
    
    
val item = it.next()
println("读取集合元素: $item")
}
}

2. Variable list

fun main(args: Array<String>) {
    
    
val list1 = mutableListOf(1, 3, 34, 54, 75) ①
val list2 = mutableListOf<String>() ②
val list3 = arrayListOf<Long?>() ③
val list4 = arrayListOf("B", "D", "F") ④
val b = "B"
// 向list2集合中添加元素
list2.add("A")
list2.add(b) ⑤
list2.add("C")
list2.add(b) ⑥
list2.add("D")
list2.add("E")
// 打印集合元素个数
println("集合size = ${list2.size}")//6 ⑦
// 打印集合
println(list2)
// 删除集合中第一个"B"元素
list2.remove(b)
// 判断集合中是否包含"B"元素
println("""是否包含"B":${list2.contains(b)}""")//true
// 判断集合是否为空
println("集合是空的:${list2.isEmpty()}")//false
// 清空集合
list2.clear()
println(list2.isEmpty())//true
// 向list3集合中添加元素
list3.add(3)
list3.add(4)
308
list3.add(6)
// 1.使用for循环遍历
println("--1.使用for循环遍历--")
for (item in list2) {
    
    
println("读取集合元素: $item")
}
// 2.使用迭代器遍历
println("--2.使用迭代器遍历--")
val it = list3.iterator()
while (it.hasNext()) {
    
    
val item = it.next()
println("读取集合元素: $item")
}
}

3. Map collection

A Map collection represents a very complex collection that allows elements to be accessed by a key. The Map collection is
composed of two collections, one is the key (key) collection and the other is the value (value) collection. The key collection is of type Set, so there
cannot be duplicate elements

1. Immutable Map Collection

fun main(args: Array<String>) {
    
    
val map1 = mapOf(102 to "张三", 105 to "李四", 109 to "王五") ①
val map2 = mapOf<Int, String>() ②
val map3 = mapOf(1 to 200)// 打印集合元素个数
println("集合size = " + map1.size) //3 ④
// 打印集合
println(map1)//{102=张三, 105=李四, 109=王五}
// 通过键取值
println("102 - ${map1[102]}")//102 - 张三 ⑤
println("105 - ${map1[105]}")//105 - 李四
// 判断键集合中是否包含109
println("键集合中是否包含109:${map1.containsKey(109)}")//true
// 判断值集合中是否包含 "李四"
println("值集合中是否包含\"李四\":${map1.containsValue("李四")}")//true
// 判断集合是否为空
println("集合是空的:" + map2.isEmpty())//true
// 1.使用for循环遍历
println("--1.使用for循环遍历--")
// 获得键集合
val keys = map1.keys ⑥
for (key in keys) {
    
    
println("key=${key} - value=${map1[key]}")
}
// 2.使用迭代器遍历
println("--2.使用迭代器遍历--")
// 获得值集合
val values = map1.values ⑦
// 遍历值集合
val it = values.iterator()
while (it.hasNext()) {
    
    
val item = it.next()
println("值集合元素: $item")
}
}

2. Variable Map collection

fun main(args: Array<String>) {
    
    
val map1 = mutableMapOf<Int, String>() ①
val map2 = mutableMapOf(1 to 102, 2 to 360) ②
val map3 = hashMapOf<Long, String>() ③
val map4 = hashMapOf("R" to "Read", "C" to "Create") ④
map1.put(102, "张三") ⑤
map1[105] = "李四" ⑥
map1[109] = "王五"
map1[110] = "董六"
//"李四"值重复
map1[111] = "李四"//109键已经存在,替换原来值"王五"
map1[109] = "刘备"// 打印集合元素个数
println("集合size = " + map1.size)//5
// 打印集合
println(map1)//{102=张三, 105=李四, 109=刘备, 110=董六, 111=李四}
// 删除键值对
map1.remove(109)// 判断键集合中是否包含109
println("键集合中是否包含109:${map1.containsKey(109)}")//false
// 判断值集合中是否包含 "李四"
println("值集合中是否包含\"李四\":${map1.containsValue("李四")}")//true
// 判断集合是否为空
println("集合是空的:" + map2.isEmpty())//false
// 清空集合
map1.clear()// 打印集合
println(map1)//{}

Reference: Kotlin from Xiaobai to Da Niu

Guess you like

Origin blog.csdn.net/baidu_31956557/article/details/109297470