当前位置:网站首页>有趣的 Kotlin 0x06:List minus list
有趣的 Kotlin 0x06:List minus list
2022-07-28 15:42:00 【AndroidKt】
最近在 http://portal.kotlin-academy.com/#/ 上看到很多关于 Kotlin 的有趣的题目。个人觉得很适合 Kotlin 爱好者,感兴趣的小伙伴可自行查阅。
【有趣的 Kotlin 】系列记录自己对每一题的理解。
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))
}
以上代码,运行结果是什么?可选项:
- [2, 3][2, 3][1, 1][1, 1]
- [2, 3][2, 3][1, 1][]
- [2, 3][2, 3][][1, 1]
- [2, 3][2, 3][][]
思考一下,记录下你心中的答案。
分析
题目如上,熟悉 Kotlin 集合相关操作符逻辑的朋友应该能很快能得到答案,这里只涉及 - 一个运算符和两个重载实现。
第一个重载实现:

逻辑就是过滤掉集合中第一个与传入参数 element 相等的元素。
第二个重载实现:

逻辑就是过滤掉与第二个列表中的任意元素相等的所有元素。
题中代码等价于如下写法:
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)))
}
逐个查看运算逻辑
val list = listOf(1, 2, 3)
print(list - 1) // 过滤掉第一个等于 1 的元素,结果为 [2, 3]
print(list - listOf(1)) // 过滤掉所有等于 1 的元素,结果为 [2, 3]
val ones = listOf(1, 1, 1)
print(ones - 1) // 过滤掉第一个等于 1 的元素,结果为 [2, 3]
print(ones - listOf(1)) // 过滤掉所有等于 1 的元素,结果为 []
所以,正确答案为
选项 2 :[2, 3][2, 3][1, 1][]
延伸
Kotlin 中使用 operator 关键字用于修饰函数,表示该函数重载一个操作符或者实现一个约定。使用 operator 关键字修饰函数并且函数名只能为component1、component2、component3 … 时则是实现一个约定,即 解构 。
顺便看看 List 的一些其他操作符(in 、+)以及 解构约定。
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())
}
运行结果如下:

componentN 最多支持到 5, 且 receiver 为 List<T>,内部其实调用的就是 get() 函数,所以当索引超出列表长度时,运行时报错。
针对 operator 和 componentN 的配合方式,举例说明一下:
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)
}
运行结果:

针对操作符重载,也举例说明下:
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)
}
运行结果如下:

注意一下:
equals操作符重载只能作为类成员函数实现,因为此函数已在Any类中定义。
编写过程中, Android Studio 会给予我们友好的提示,不愧是 YYDS 。

总结
- 操作符重载
- 解构
边栏推荐
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- 解决uniapp等富文本图片宽度溢出
- ANSA二次开发 - Apps和ANSA插件管理
- About mit6.828_ HW9_ Some problems of barriers xv6 homework9
- Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
- I can only sell the company after the capital has been "cut off" for two years
- Splash (渲染JS服务)介绍安装
- ANSA二次开发 - 抽中面的两种方法
- Debugging methods of USB products (fx3, ccg3pa)
- Introduction and implementation of queue (detailed explanation)
猜你喜欢

HM secondary development - data names and its use

Sort 1-insert sort and Hill sort

FX3开发板 及 原理图

Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment

Debugging methods of USB products (fx3, ccg3pa)

Learn ABAQUS script programming script in an hour

Record doc

Using pyqt to design gui in ABAQUS

栈的介绍与实现(详解)

魏建军骑宝马也追不上李书福
随机推荐
PHP mb_substr 中文乱码
Thoughts on solving the pop-up of malicious computer advertisements
局域网无法访问apache服务器
解决电脑恶意广告弹窗的思路
Automatic conversion and cast
Microsoft question 100 - do it every day - question 11
SCI scientific paper writing Growth Camp (full version)
Baidu editor ueeditor, when editing too much content, the toolbar is not visible, which is not convenient for editing or uploading problems
微信公众号获取素材列表
HyperMesh运行脚本文件的几种方法
Li Hongyi, machine learning 5. Tips for neural network design
ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境
【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
Automatically pack compressed backup download and delete bat script commands
HyperMesh自动保存(增强版)插件使用说明
8051 series MCU firmware upgrade IAP
Qt学习第一天
LwIP development | socket | TCP | client
Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
LwIP development | realize TCP server through socket