简体   繁体   English

可以在java中使用kotlin中可变的列表类型吗?

[英]what list type which is mutable in kotlin can be used in java?

Having a lib created with kotlin, in it there is a function expected to be overriden by its descendent class, which takes a mutableList (expected to be modified in this function) 有一个用kotlin创建的lib,其中有一个函数可以被它的后代类覆盖,它接受一个mutableList(希望在这个函数中被修改)

protected open fun makeDataForAdapter(itemsList: MutableList<Data>) : List<Data>{
    return itemsList // default behavior
}

the lib is used in a app with java, so the descendent class overrides it: lib在java中的应用程序中使用,因此后代类会覆盖它:

@Override
protected List<Data> makeDataForAdapter(MutableList<Data> itemsList) {
     ......
}

the compiler complains about "cant resolve the MutableList" 编译器抱怨"cant resolve the MutableList"

what list type which is mutable in kotlin can be used in java? 可以在java中使用kotlin中可变的列表类型吗?

The kotlin.List<out T> and kotlin.MutableList<E> are mapped types , which, on the JVM and Android, are both represented by the java.util.List<E> interface. kotlin.List<out T>kotlin.MutableList<E> 是映射类型 ,它们在JVM和Android上都由java.util.List<E>接口表示。

So you just need to use the Java List<E> type wherever you need to inter-operate with the Kotlin List<out E> or MutableList<E> . 所以你只需要在需要与Kotlin List<out E>MutableList<E>交互操作的地方使用Java List<E>类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM