我是Kotlin新手,正在尝试将一个用Scala编写的应用程序转换为Kotlin。在Scala中,有来自scala.collection.mutable.ListBuffer的ListBuffer。在Kotlin中有类似的东西吗?我尝试导入com.sun.tools.javac.util.ListBuffer,但在尝试编译时出现错误:Kotlin: Symbol is declared in module 'jdk.compiler' which does not export package 'com.sun.tools.javac.util'
我有一个Java/Kotlin互操作的问题。一个Kotlin不可变的列表被编译成一个正常的java.util.ArrayList,它是可变的!
Kotlin (库):
class A {
val items: List<Item> = ArrayList()
}
Java (消费者):
A a = new A();
a.getItems().add(new Item()); // Compiles and runs but I wish to fail or throw
如何让我的Kotlin类也从Java的角度完全不可变?
我声明了一个这样的变量:
var G: Array<MutableList<Int>> = Array(0) { ArrayList() }
Kotlin给出了以下错误:
Kotlin: Type inference failed: Not enough information to infer parameter E in fun <E> <init>(): kotlin.collections.ArrayList<E> /* = java.util.ArrayList<E> */
Please specify it e
考虑下面的Java类..。
public class JavaSuperClassWithInstanceMembers {
private final int superClassReadonlyProperty = 0;
private int superClassMutableProperty = 0;
public int getSuperClassReadonlyProperty() {
return superClassReadonlyProperty;
}
public int getSuperClassMutableP
我试图使用Kotlin中的注释来定义一个简单的Hibernate映射。但我的多对多关系并没有像预期的那样起作用。导致IntelliJ IDEA中出现以下错误:
‘一对多’/‘多到多’属性值类型不应该是'?扩展PersonAddress/Person‘
我的代码库:
@Entity
open class Person(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
var id: Long = 0L,
@OneToMany(mappedBy = "
我有一个js互操作函数,它使用for in构造来迭代输入元素,但它在运行时抛出了错误。
native("document")
val ndoc: dynamic = noImpl
fun jsInterOp() {
js("console.log('JS inling from kotlin')")
val ies = ndoc.getElementsByTagName("input")
for (e in ies) {
console.log("Input element ID:
我尝试使用Kotlin (Multiplatform)开发安卓和iOS之间的共享库。但是我在iOS中面临线程问题。对于我在iOS中的应用程序,我在主线程中建立了对象,但它可能会调用另一个线程中的函数,并按如下方式抛出这个异常:
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared example.api.DrivingBehaviorDetector@397cba8 from other thread
at 0
无法在android中使用kotlin作用域函数,显示未解决的引用。工作室版本:北极狐2020.3.1补丁4
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
def HILT_VERSION ="2.38.1"
def NAV_V
(注意:下面有一个潜在的破坏者- Kotlin Koans )
给定一个高阶函数,该函数采用如下所示的函数文字:
fun <K, V> buildMap(build: MutableMap<K, V>.() -> Unit): Map<K, V> {
// How does java.util.HashMap satisfy the MutableMap interface?
// Does Kotlin support ducktyping?
val map = HashMap<K, V>()
map.bu
在Kotlin中,我有如下代码:
open class A {
protected open fun B.doSomething() {}
}
class B {}
class C : A() {
override fun B.doSomething() {
print("first, do some C-specific logic")
print("now try to call super implementation. How?")
super.doSomething() // doe