当前位置:网站首页>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
边栏推荐
- Programming implementation of ROS learning 6 -service node
- 编辑器-vi、vim的使用
- 2011-11-21 training record personal training (III)
- C#绘制带控制点的Bezier曲线,用于点阵图像及矢量图形
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- notepad++
- Explain NN in pytorch in simple terms CrossEntropyLoss
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- 2311. Longest binary subsequence less than or equal to K
- 【阅读笔记】图对比学习 GNN+CL
猜你喜欢
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
利用请求头开发多端应用
Programming implementation of ROS learning 6 -service node
Nodemon installation and use
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
【阅读笔记】图对比学习 GNN+CL
TF coordinate transformation of common components of ros-9 ROS
OpenGL - Coordinate Systems
Priority queue (heap)
随机推荐
Ros-11 common visualization tools
Golang foundation - the time data inserted by golang into MySQL is inconsistent with the local time
ROS learning 4 custom message
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
ROS learning 1- create workspaces and function packs
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
np. allclose
Wxml template syntax
12. Dynamic link library, DLL
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
2310. The number of bits is the sum of integers of K
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Codeworks round 681 (Div. 2) supplement
【阅读笔记】图对比学习 GNN+CL
Confusion matrix
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Applet (use of NPM package)
AdaBoost use
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]