当前位置:网站首页>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
边栏推荐
- MySQL5.7及SQLyogV12安装及使用破解及常用命令
- ANSYS secondary development - MFC interface calls ADPL file
- Splash (rendering JS service) introduction installation
- Leetcode daily practice - the number of digits in the offer 56 array of the sword finger
- Wei Jianjun couldn't catch up with Li Shufu by riding a BMW
- egg(十九):使用egg-redis性能优化,缓存数据提升响应效率
- ANSA二次开发 - Apps和ANSA插件管理
- Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
- 大地坐标系转换火星坐标系
- The little red book of accelerating investment, "rush to medical treatment"?
猜你喜欢

Thoughts on solving the pop-up of malicious computer advertisements

Learn ABAQUS script programming script in an hour

Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult

ANSYS secondary development - MFC interface calls ADPL file

LeetCode-学会对无序链表进行插入排序(详解)

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

Rosen's QT journey 101 models and views in QT quick

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code

Ansa secondary development - Introduction to interface development tools

ANSA二次开发 - Apps和ANSA插件管理
随机推荐
CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
Microsoft question 100 - do it every day - question 16
Ansa secondary development - two methods of drawing the middle surface
局域网无法访问apache服务器
2021-04-02
Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)
Geodetic coordinate system to Martian coordinate system
排序5-计数排序
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Rosen's QT journey 102 listmodel
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
排序3-选择排序与归并排序(递归实现+非递归实现)
First day of QT study
About standard IO buffers
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
Ansa secondary development - Introduction to interface development tools
MySQL view event status statements and modification methods
信号屏蔽与处理
Installation of QT learning