相关疑难解决方法(0)

Kotlin二级构造函数

如何在Kotlin中声明辅助构造函数?

有没有关于这方面的文件?

以下不编译......

class C(a : Int) {
  // Secondary constructor
  this(s : String) : this(s.length) { ... }
}
Run Code Online (Sandbox Code Playgroud)

syntax constructor kotlin

91
推荐指数
7
解决办法
5万
查看次数

Kotlin:泛型数组

我正在写一棵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对于我的目标来说是一个很好的解决方案。

arrays generics kotlin

4
推荐指数
1
解决办法
2532
查看次数

标签 统计

kotlin ×2

arrays ×1

constructor ×1

generics ×1

syntax ×1