如何在Kotlin中声明辅助构造函数?
有没有关于这方面的文件?
以下不编译......
class C(a : Int) {
// Secondary constructor
this(s : String) : this(s.length) { ... }
}
Run Code Online (Sandbox Code Playgroud) 我正在写一棵B树,它的一个节点可能有很多键,我遇到了一个问题。当我创建一个Int数组时,一切正常:
class Node<K: Comparable<K>> (val t: Int) {
val keys: Array<Int?> = Array<Int?> (t*2-1, {null})
}
Run Code Online (Sandbox Code Playgroud)
但我想创建一个泛型K数组:
class Node<K: Comparable<K>> (val t: Int) {
val keys : Array<K?> = Array<K?> (t*2-1, {null})
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,编译器会抛出以下错误消息:
'Kotlin: Cannot use 'K' as reified type parameter. Use a class instead.'
Run Code Online (Sandbox Code Playgroud)
问题是如何创建泛型数组?
UPD:感谢所有回复!看来MutableList对于我的目标来说是一个很好的解决方案。