当前位置:网站首页>Kotlin introductory notes (VIII) collection and traversal
Kotlin introductory notes (VIII) collection and traversal
2022-07-05 09:14:00 【Meng Meng Mu Xi】
Preface : This tutorial is best done with JAVA Study on the basis of
One 、List aggregate
ListOf()
Press Java Of List How to write it ,Kotlin should :
val list = ArrayList<String>()
list.add("Apple")
list.add("Banana")
list.add("Orange")
list.add("Pear")
list.add("Grape")
however Kotlin Specially built-in listOf() Function to simplify the writing of the initialization set :
val list = listOf("Apple","Banana","Orange","Pear","Grape")
It can be used for-in loop Traversal .
fun main() {
val list = listOf("Apple","Banana","Orange","Pear","Grape")
for (fruit in list) {
println(fruit)
}
}
But here's the thing ,listOf() Function creates an immutable collection .( Unable to add collection 、 Modify or delete operations )
mutableListOf()
This design and val keyword 、 Class is not inheritable The original intention of the design is similar ,Kotlin In terms of immutability, the control is extremely strict . If you create a variable set , have access to mutableListOf() Function is OK , Examples are as follows :
fun main() {
val list = mutableListOf("Apple","Banana","Orange","Pear","Grape")
list.add("Watermenol")
for (fruit in list) {
println(fruit)
}
}
So let's use this mutableListOf() Function to create a mutable collection , Then a new fruit is added to the collection , Final use for-in loop The collection is traversed .
(2) Set aggregate
setOf()
val set = setOf("Apple","Banana","Orange","Pear","Grape")
for (fruit in set) {
println(fruit)
}
mutableSetOf()
Set Of setOf() and mutableSetOf() And List A collection of listOf() and mutableListOf() The general usage is the same , But tell me more .
It should be noted that , Set The bottom layer is to use hash Mapping mechanism to store data , Therefore, the elements in the collection cannot be guaranteed to be orderly , This is the List The biggest difference .
Map aggregate
Map It's a kind of “ Key value pair ” Data structure in form , Therefore, the usage is similar to List、Set Sets differ greatly .
according to Java Writing :
val map = HashMap<String , Int>()
map.put("Apple",1)
map.put("Banana",2)
map.put("Orange",3)
map.put("Pear",4)
map.put("Grape",5)
But in Kotlin It is more recommended to use a syntax structure similar to array subscript ,
For example Map You can add a piece of data in :
map["Apple"] = 1
Read Map The structure in can be written like this :
val num = map["Apple"]
therefore , The above code can be written as :
val map = HashMap<String , Int>()
map["Apple"] = 1
map["Banana"] = 2
map["Orange"] = 3
map["Pear"] = 4
map["Grape"] = 5
however , according to Kotlin The usual style . . . . you 're right , It provides a pair of mapOf() And mutableMapOf() Function to continue simplification Map Usage of . stay mapOf() in , We can directly pass in the initialized key value pair combination to complete the pair Map Set creation :
val map = mapOf ("Apple" to 1 , "Banana" to 2 , "Orange" to 3 , "Pear" to 4 , "Grape" to 5)
The key value pair combination here looks like to This keyword is used for association , But in fact to It's not a keyword , It is a infix function ( Put this temporarily , Will be discussed later )
fun main() {
val map = mapOf("Apple" to 1 , "Banana" to 2 , "Orange" to 3 , "Pear" to 4 , "Grape" to 5)
for((fruit , number) in map) {
println("fruit is " + fruit + ", number is " + number)
}
}
If you like this series , You might as well pay attention ! Thank you for watching !
Reference resources :
《 First line of code Android ( The third edition )》 --- Guo Lin
边栏推荐
- Priority queue (heap)
- Confusing basic concepts member variables local variables global variables
- My experience from technology to product manager
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- Nodemon installation and use
- Jenkins Pipeline 方法(函数)定义及调用
- Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
- Composition of applet code
- 信息与熵,你想知道的都在这里了
- Hi Fun Summer, play SQL planner with starrocks!
猜你喜欢
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
c语言指针深入理解
Priority queue (heap)
Newton iterative method (solving nonlinear equations)
Introduction Guide to stereo vision (1): coordinate system and camera parameters
【ManageEngine】如何利用好OpManager的报表功能
Using request headers to develop multi terminal applications
L'information et l'entropie, tout ce que vous voulez savoir est ici.
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Codeworks round 639 (Div. 2) cute new problem solution
随机推荐
优先级队列(堆)
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
C [essential skills] use of configurationmanager class (use of file app.config)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
fs. Path module
Using request headers to develop multi terminal applications
Nodejs modularization
我的一生.
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
牛顿迭代法(解非线性方程)
It's too difficult to use. Long articles plus pictures and texts will only be written in short articles in the future
Applet global style configuration window
2311. 小于等于 K 的最长二进制子序列
Array, date, string object method
Understanding rotation matrix R from the perspective of base transformation
Codeforces Round #648 (Div. 2) D. Solve The Maze
Priority queue (heap)
OpenGL - Model Loading
Pearson correlation coefficient