除了 How to write a basic swap function in Java 中描述的 java 方式之外,还有没有更好的方式在 kotlin 中编写通用交换函数? .
有没有什么kotlin语言特性可以让通用的swap函数更加简洁直观?
最佳答案
在 Kotlin 中根本不需要交换函数。您可以使用现有的 also函数,例如:
var a = 1
var b = 2
a = b.also { b = a }
println(a) // print 2
println(b) // print 1
https://stackoverflow.com/questions/45377802/