Kotlin 简明教程
Kotlin - Lists
Kotlin 列表是有序的项目集合。Kotlin 列表可以是可变的 ( mutableListOf ) 或只读的 ( listOf )。可以使用索引访问列表的元素。Kotlin 可变或不可变列表可以有重复的元素。
Kotlin list is an ordered collection of items. A Kotlin list can be either mutable (mutableListOf) or read-only (listOf). The elements of list can be accessed using indices. Kotlin mutable or immutable lists can have duplicate elements.
Creating Kotlin Lists
对于列表创建,使用标准库函数 listOf() (用于只读列表)和 mutableListOf() (用于可变列表)。
For list creation, use the standard library functions listOf() for read-only lists and mutableListOf() for mutable lists.
Example
fun main() {
val theList = listOf("one", "two", "three", "four")
println(theList)
val theMutableList = mutableListOf("one", "two", "three", "four")
println(theMutableList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four]
[one, two, three, four]
Loop through Kotlin Lists
遍历 Kotlin 列表有多种方法。让我们逐一学习:
There are various ways to loop through a Kotlin list. Lets study them one by one:
Using toString() function
fun main() {
val theList = listOf("one", "two", "three", "four")
println(theList.toString())
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four]
Using Iterator
fun main() {
val theList = listOf("one", "two", "three", "four")
val itr = theList.listIterator()
while (itr.hasNext()) {
println(itr.next())
}
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
one
two
three
four
Size of Kotlin List
我们可以使用 size 属性来计算列表内的元素总数:
We can use size property to get the total number of elements in a list:
fun main() {
val theList = listOf("one", "two", null, "four", "five")
println("Size of the list " + theList.size)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Size of the list 5
The "in" Operator
可以使用 in 运算符来检查列表内是否存在某个元素。
The in operator can be used to check the existence of an element in a list.
The contain() Method
还可以使用 contain() 方法来检查列表内是否存在某个元素。
The contain() method can also be used to check the existence of an element in a list.
The isEmpty() Method
isEmpty() 方法在集合为空(不包含任何元素)时返回 true ,否则返回 false 。
The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise.
The indexOf() Method
indexOf() 方法返回列表中指定元素首次出现的索引,如果 lista 不包含指定元素,则返回 -1。
The indexOf() method returns the index of the first occurrence of the specified element in the list, or -1 if the specified element is not contained in the list.
The get() Method
可以使用 get() 方法来获取列表中指定索引处的元素。第一个元素的索引为零。
The get() method can be used to get the element at the specified index in the list. First element index will be zero.
List Addition
我们可以使用 + 运算符将两个或多个列表添加到一个列表中。这会将第二个列表添加到第一个列表中,甚至会添加重复元素。
We can use + operator to add two or more lists into a single list. This will add second list into first list, even duplicate elements will also be added.
Example
fun main() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("four", "five", "six")
val resultList = firstList + secondList
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four, five, six]
List Subtraction
我们可以使用 - 运算符从另一个列表中减去一个列表。此操作会从第一个列表中移除公共元素并返回结果。
We can use - operator to subtract a list from another list. This operation will remove the common elements from the first list and will return the result.
Slicing a List
我们可以使用 slice() 方法从给定列表中获取子列表,该方法使用元素索引的范围。
We can obtain a sublist from a given list using slice() method which makes use of range of the elements indices.
Removing null a List
我们可以使用 filterNotNull() 方法从 Kotlin 列表中移除 null 元素。
We can use filterNotNull() method to remove null elements from a Kotlin list.
fun main() {
val theList = listOf("one", "two", null, "four", "five")
val resultList = theList.filterNotNull()
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, four, five]
Filtering Elements
我们可以使用 filter() 方法过滤出与给定谓词匹配的元素。
We can use filter() method to filter out the elements matching with the given predicate.
fun main() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.filter{ it > 30}
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[31, 40, 50]
Dropping First N Elements
我们可以使用 drop() 方法从列表中丢弃前 N 个元素。
We can use drop() method to drop first N elements from the list.
fun main() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.drop(3)
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[31, 40, 50, -1, 0]
Grouping List Elements
我们可以使用 groupBy() 方法对与给定谓词匹配的元素进行分组。
We can use groupBy() method to group the elements matching with the given predicate.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.groupBy{ it % 3}
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
{1=[10, 31, 40], 0=[12, 30, 9, -3, 0]}
Mapping List Elements
我们可以使用 map() 方法使用提供的函数映射所有元素:
We can use map() method to map all elements using the provided function:.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.map{ it / 3 }
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[3, 4, 10, 10, 13, 3, -1, 0]
Chunking List Elements
我们可以使用 chunked() 方法从列表中创建给定大小的块。最后一个块可能没有等于块大小的元素,具体取决于列表中的元素总数。
We can use chunked() method to create chunks of the given size from a list. Last chunk may not have the elements equal to the number of chunk size based on the total number of elements in the list.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.chunked(3)
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [31, 40, 9], [-3, 0]]
Windowing List Elements
我们可以使用 windowed() 方法创建元素范围列表,通过将固定大小的滑动窗口移过元素集合来实现。
We can use windowed() method to a list of element ranges by moving a sliding window of a given size over a collection of elements.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3)
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [12, 30, 31], [30, 31, 40], [31, 40, 9], [40, 9, -3], [9, -3, 0]]
默认情况下,滑动窗口每次移动一步,但我们可以通过传递自定义步长值来更改此设置:
By default, the sliding window moves one step further each time but we can change that by passing a custom step value:
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3, 3)
println(resultList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [31, 40, 9]]
Kotlin mutable List
我们可以使用 mutableListOf() 创建可变列表,稍后我们可以使用 add() 向同一列表中添加更多元素,并且可以使用 remove() 方法从列表中移除元素。
We can create mutable list using mutableListOf(), later we can use add() to add more elements in the same list, and we can use remove() method to remove the elements from the list.
fun main() {
val theList = mutableSetOf(10, 20, 30)
theList.add(40)
theList.add(50)
println(theList)
theList.remove(10)
theList.remove(30)
println(theList)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[10, 20, 30, 40, 50]
[20, 40, 50]