BigInteger在Kotlin
我需要使用BigInteger,但在kotlin中找不到类似的东西。
在java的BigInteger的kotlin中有没有其他类?
要么
我应该将java类导入kotlin吗?
java.math.BigInteger
可以像任何其他Java类一样在Kotlin中使用。 stdlib中甚至有一些助手可以使通用操作更易于读写。 你甚至可以自己扩展帮助者以获得更好的可读性:
import java.math.BigInteger fun Long.toBigInteger() = BigInteger.valueOf(this) fun Int.toBigInteger() = BigInteger.valueOf(toLong()) val a = BigInteger("1") val b = 12.toBigInteger() val c = 2L.toBigInteger() fun main(argv:Array<String>){ println((a + b)/c) // prints out 6 }
你可以使用Kotlin的任何内置的Java类,你应该。 他们的工作方式与Java中的完全相同。 Kotlin重点介绍了Java平台提供的功能,而不是重新实现它们; 例如,没有Kotlin特定的集合,只有Java集合上的一些接口,而标准库也使用这些集合。
所以是的,你应该只使用java.math.BigInteger
。 作为奖励,当你使用Kotlin的BigInteger
时,你实际上可以使用操作符而不是函数调用: +
而不是add
, -
而不是subtract
等。