当前位置:网站首页>Interesting kotlin 0x06:list minus list
Interesting kotlin 0x06:list minus list
2022-07-28 16:45:00 【AndroidKt】
Recently http://portal.kotlin-academy.com/#/ I see a lot about Kotlin An interesting topic . I think it's very suitable for Kotlin lovers , If you are interested, you can check it by yourself .
【 amusing Kotlin 】 Record your understanding of each question in the series .
0x06:List minus list
fun main(args: Array<String>) {
val list = listOf(1, 2, 3)
print(list - 1)
print(list - listOf(1))
val ones = listOf(1, 1, 1)
print(ones - 1)
print(ones - listOf(1))
}
Above code , What is the result of the operation ? optional :
- [2, 3][2, 3][1, 1][1, 1]
- [2, 3][2, 3][1, 1][]
- [2, 3][2, 3][][1, 1]
- [2, 3][2, 3][][]
Think about it , Record the answers in your mind .
analysis
The title is as above , be familiar with Kotlin Set related operator logic friends should be able to get the answer soon , It's just about - One operator and two overloaded implementations .
The first overload implementation :

The logic is to filter out the first and incoming parameters in the set element Equal elements .
The second overload implementation :

Logic is to filter out all elements that are equal to any element in the second list .
The code in the question is equivalent to the following :
fun main(args: Array<String>) {
val list = listOf(1, 2, 3)
print(list.minus(1))
print(list.minus(listOf(1)))
val ones = listOf(1, 1, 1)
print(ones.minus(1))
print(ones.minus(listOf(1)))
}
Look at the logic one by one
val list = listOf(1, 2, 3)
print(list - 1) // Filtering out the first equals 1 The elements of , The result is [2, 3]
print(list - listOf(1)) // Filter out everything that's equal to 1 The elements of , The result is [2, 3]
val ones = listOf(1, 1, 1)
print(ones - 1) // Filtering out the first equals 1 The elements of , The result is [2, 3]
print(ones - listOf(1)) // Filter out everything that's equal to 1 The elements of , The result is []
therefore , The correct answer is
Options 2 :[2, 3][2, 3][1, 1][]
extend
Kotlin Use in operator Keywords are used to modify functions , Indicates that the function overloads an operator or implements a convention . Use operator Keyword modifies function and function name can only be component1、component2、component3 … When it comes to achieving an agreement , namely deconstruction .
By the way List Some other operators of (in 、+) as well as Deconstruction agreement .
fun main() {
val list = listOf(1, 2, 3)
println(2 in list)
println(4 in list)
println(list + 5)
println(list + listOf(1, 2, 3))
println(list + arrayOf(1, 2, 3))
println(list + sequenceOf(1, 2, 3))
val (v, w, x) = list
println(v)
println(w)
println(x)
println(list.component1())
println(list.component2())
println(list.component3())
println(list.component4())
println(list.component5())
}
The operation results are as follows :

componentN Up to 5, And receiver by List<T>, The internal call is actually get() function , So when the index exceeds the length of the list , Run time error .
in the light of operator and componentN It's the way we work together , For example :
class Location(val x: Int, val y: Int) {
operator fun component1() = x
operator fun component2() = y
}
fun main() {
val location = Location(520, 1314)
val (x, y) = location
println(x)
println(y)
}
Running results :

For operator overloading , For example :
operator fun Location.minus(location: Location): Location {
return Location(this.x - location.x, this.y - location.y)
}
operator fun Location.plus(location: Location): Location {
return Location(this.x + location.x, this.y + location.y)
}
operator fun Location.contains(location: Location): Boolean {
return this.x > location.x && this.y > location.y
}
class Location(val x: Int, val y: Int) {
operator fun component1() = x
operator fun component2() = y
override operator fun equals(other: Any?): Boolean =
other is Location && this.x == other.x && this.y == other.y
override fun hashCode(): Int {
var result = x
result = 31 * result + y
return result
}
override fun toString(): String {
return "($x, $y)"
}
}
fun main() {
val location = Location(520, 1314)
val (x, y) = location
println(x)
println(y)
val other = Location(1, 2)
println(location - other)
println(location + other)
println(location == other)
println(other in location)
}
The operation results are as follows :

Pay attention to the :
equalsOperator overloading can only be implemented as a class member function , Because this function is already inAnyDefinition in class .
In the process of writing , Android Studio Will give us friendly tips , Not the kui is a YYDS .

summary
- operators overloading
- deconstruction
边栏推荐
- Applet: get element node information
- LeetCode-学会复杂带随机指针链表的题(详解)
- Microsoft question 100 - do it every day - question 16
- Some suggestions on optimizing HyperMesh script performance
- Li Hongyi, machine learning 5. Tips for neural network design
- Microsoft question 100 - do it every day - question 11
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 a.ancestor lca+ violence count
- PHP 图片上传
- The epidemic dividend disappeared, and the "home fitness" foam dissipated
- 快速掌握 Kotlin 集合函数
猜你喜欢

有趣的 Kotlin 0x0A:Fun with composition

USB产品(FX3、CCG3PA)的调试方法

Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server

小程序:获取元素节点信息

Sort 5-count sort

I can only sell the company after the capital has been "cut off" for two years

LeetCode-学会复杂带随机指针链表的题(详解)

nowcode-学会删除链表中重复元素两题(详解)

The epidemic dividend disappeared, and the "home fitness" foam dissipated

Rosen's QT journey 101 models and views in QT quick
随机推荐
IM即时通讯开发优化提升连接成功率、速度等
Redis系列4:高可用之Sentinel(哨兵模式)
PHP image synthesis technology
微信公众号获取素材列表
Using pyqt to design gui in ABAQUS
ANSYS二次开发 - MFC界面调用ADPL文件
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
Qt学习第一天
使用js直传oss阿里云存储文件,解决大文件上传服务器限制
Detailed explanation of QT qstring
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)
解决uniapp等富文本图片宽度溢出
ANSA二次开发 - 抽中面的两种方法
Applet: get element node information
WSL+Valgrind+Clion
一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
2021-04-02
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
Configure HyperMesh secondary development environment on vs Code
Qt学习之安装