以下是一些常见的用法示例:
fun main() {
val array1 = arrayOf(1, 2, 3)
val array2 = arrayOf(4, 5, 6)
val mergedArray = array1.plus(array2)
println(mergedArray.contentToString()) // 输出 [1, 2, 3, 4, 5, 6]
}
val array = arrayOf(1, 2, 3, 4, 5)
val newArray = array.minus(3) // 移除元素 3
println(newArray.contentToString()) // 输出 [1, 2, 4, 5]
val array = arrayOf(1, 2, 3, 4, 5)
val slicedArray = array.slice(1..3) // 获取索引为 1 到 3 的元素
println(slicedArray.contentToString()) // 输出 [2, 3, 4]
val array = arrayOf(1, 2, 3, 4, 5)
val chunkedList = array.chunked(2) // 将数组分成大小为 2 的块
println(chunkedList) // 输出 [[1, 2], [3, 4], [5]]
val array = arrayOf(1, 2, 2, 3, 3, 4, 5)
val distinctArray = array.distinct() // 获取不同的元素
println(distinctArray.contentToString()) // 输出 [1, 2, 3, 4, 5]
find():用于在数组中查找符合指定条件的元素,并返回该元素
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.find { it % 2 == 0 }
println(result) // 输出:2
fun main() {
val array = arrayOf("Hello", "World")
val result = array.joinToString()
println(result) // 输出 "HelloWorld"
}
fun String.toUpperCase(): String {
return this.toUpperCase()
}
val strings = listOf("apple", "banana", "cherry")
// 使用自定义的 toUpperCase() 函数将字符串转换为大写
val upperCaseStrings = strings.map { it.toUpperCase() }
println(upperCaseStrings) // 输出 [APPLE, BANANA, CHERRY]
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
// 使用 sum() 函数计算所有数字的总和
val sum = numbers.sum()
println("Sum of numbers: $sum") // 输出 "Sum of numbers: 15"
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
// 使用 first() 函数获取第一个数字
val firstNumber = numbers.first()
println("First number: $firstNumber") // 输出 "First number: 1"
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
// 使用 last() 函数获取最后一个数字
val lastNumber = numbers.last()
println("Last number: $lastNumber") // 输出 "Last number: 5"
}
val map = mapOf("apple" to 1, "banana" to 2, "orange" to 3)
// 获取键为 "apple" 的值,如果不存在则返回 null
val appleValue = map.getOrNull("apple")
println("Apple value: $appleValue") // 输出 "Apple value: 1"
// 获取键为 "grape" 的值,如果不存在则返回 null
val grapeValue = map.getOrNull("grape")
println("Grape value: $grapeValue") // 输出 "Grape value: null"
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 使用 take() 函数获取前三个数字
val result = numbers.take(3)
// 遍历并打印结果
for (num in result) {
println(num)
}
}
我们使用循环遍历并打印结果。输出将为:
1
2
3
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 使用 takeLast() 函数获取最后三个数字
val result = numbers.takeLast(3)
// 遍历并打印结果
for (num in result) {
println(num)
}
}
输出将为:
7
8
9
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 使用 drop() 函数删除前三个数字
val result = numbers.drop(3)
// 打印删除元素后的新序列
println(result) // 输出 [4, 5, 6, 7, 8, 9, 10]
var numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9,
// 使用 dropLast() 函数删除最后三个数字并将结果分配回原始列表
numbers = numbers.dropLast(3)
// 打印修改后的原始列表
println(numbers) // 输出 [1, 2, 3, 4, 5, 6, 7]
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 使用 filter() 函数筛选出偶数并打印
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // 输出:[2, 4, 6, 8, 10]
}
fun main() {
// 创建一个包含字符串的列表
val strings = listOf("apple", "banana", "orange")
// 使用 map() 函数将字符串转换为它们的长度,并打印结果
val lengths = strings.map { it.length }
println(lengths) // 输出:[6, 6, 5]
}
fun main() {
val listOfLists = listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(7, 8, 9))
val flattenedList = listOfLists.flatMap { it }
println(flattenedList) // 输出:[1, 2, 3, 4, 5, 6, 7, 8, 9]
}
fun main() {
val numbers = listOf(1, 2, 2, 3, 4, 4, 5)
val uniqueNumbers = numbers.distinct()
println(uniqueNumbers) // 输出了 [1, 2, 3, 4, 5]
}
distinctBy():根据指定条件去除重复元素
fun main() {
val items = listOf("apple", "banana", "cherry", "date", "elderberry")
val distinctItems = items.distinctBy { it.length }
println(distinctItems) // 输出了 [apple, banana, cherry, date]
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val containsThree = numbers.contains(3)
println(containsThree) // 输出:true
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val index = numbers.indexOf(3)
println("The index of 3 is: $index") // 输出:The index of 3 is: 2
}
我们打印出结果,显示数字 3 在列表中的位置。请注意,indexOf() 函数返回的是该元素在列表中的位置,索引从 0 开始。
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 3)
val lastIndex = numbers.lastIndexOf(3)
println("The last index of 3 is: $lastIndex") // 输出:The last index of 3 is: 5
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val (evenNumbers, oddNumbers) = numbers.partition { it % 2 == 0 }
println("Even numbers: $evenNumbers") // 输出:[2, 4]
println("Odd numbers: $oddNumbers") // 输出:[1, 3, 5]
}
fun main() {
val items = listOf("apple", "banana", "cherry", "date", "elderberry")
val itemGroups = items.groupBy { it.length }
for ((key, value) in itemGroups) {
println("Items with length $key: $value")
}
}
答案输出:
Items with length 5: [apple]
Items with length 6: [banana, cherry]
Items with length 4: [date]
Items with length 10: [elderberry]
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val sum = numbers.sum()
println("The sum of the numbers is: $sum") // 输出:The sum of the numbers is: 15
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val average = numbers.average()
println("The average of the numbers is: $average") // 输出:The average of the numbers is: 3.0
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val maxNumber = numbers.max()
println("The maximum number is: $maxNumber") // 输出:The maximum number is: 5
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.reduce { acc, num -> acc + num }
println("The sum of the numbers is: $result") // 输出:The sum of the numbers is: 15
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.fold(0) { acc, num -> acc + num }
println("The sum of the numbers is: $result") // 输出:The sum of the numbers is: 15
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forEach { println(it) }
}
将会输出以下内容:
1
2
3
4
5
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forEachIndexed { index, num -> println("Index $index: $num") }
}
将会输出以下内容:
Index 0: 1
Index 1: 2
Index 2: 3
Index 3: 4
Index 4: 5
fun main() {
val list1 = listOf("a", "b", "c")
val list2 = listOf(1, 2, 3)
val zippedList = list1.zip(list2) { a, b -> Pair(a, b) }
zippedList.forEach { println(it) }
}
将会输出以下内容:
复制代码
Pair(a, 1)
Pair(b, 2)
Pair(c, 3)
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val allPositive = numbers.all { it > 0 }
println("All numbers are positive: $allPositive") // 输出:All numbers are positive: true
}
fun main() {
val numbers = listOf(1, 2, -3, 4, 5)
val hasNegative = numbers.any { it < 0 }
println("Numbers have negative value: $hasNegative") // 输出:Numbers have negative value: true
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val hasNegative = numbers.none { it < 0 }
println("Numbers have no negative value: $hasNegative") // 输出:Numbers have no negative value: true
}
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val associatedMap = numbers.associate { it to it }
println("Associated map: $associatedMap")
}
输出
Associated map: {1=1, 2=2, 3=3, 4=4, 5=5}
fun main() {
val people = listOf("David", "Bob", "Charlie", "Alice")
val associatedMap = people.associateBy { it.length }
println("Associated map: $associatedMap")
}
Associated map: {5=Alice, 3=Bob, 7=Charlie}
val people = listOf("Alice", "Bob", "Charlie", "David")
val associatedMap = people.associateWith { it.length }
println("Associated map: $associatedMap")
Associated map: {Alice=5, Bob=3, Charlie=7, David=5}
val numbers = listOf(1, 2, 3, 4, 5)
val flattened = numbers.flatMapTo(HashSet()) { setOf(it * 2) }
println("Flattened list: $flattened")
val numbers = listOf(1, 2, 3, 4, 5)
val squaredNumbers = numbers.mapTo(ArrayList()) { it * it }
println("Squared numbers: $squaredNumbers")
输出结果:Squared numbers: [1, 4, 9, 16, 25]
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.reduceIndexed { index, acc, value ->
if (index % 2 == 0) {
acc + value
} else {
acc - value
}
}
println("Result: $result")
输出结果将会是:
Result: 3
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.foldIndexed(0) { index, acc, value ->
if (index % 2 == 0) {
acc + value
} else {
acc - value
}
}
println("Result: $result")
输出结果将会是:Result: 3