我有这样的密码:
enum class Player { PLAYER, COMPUTER }
interface BoardCell {
val x: Int
val y: Int
var player: Player?
}
data class Cell(val x: Int, val y: Int, var player: Player?, var value: Int)
data class BoardCellClass(override val x: Int, override val y: Int, override var player: Player
我正在试图找出使用委托声明类的语法,该类也具有类型限制。例如,这是有效的语法:
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) : MutableList<T> by innerList
但是现在我要强制执行T扩展ClassFoo并实现InterfaceBar。我应该把where关键字放在哪里?
以下是我尝试过的一些不编译的东西:
open class Cell<T>(val innerList: MutableList<T> = mutableL
在这里,我填充了两个列表,每个列表要么是可变的,要么是不可变的:
var mutableList = scala.collection.mutable.MutableList[String]()
//> mutableList : scala.collection.mutable.MutableList[String] = MutableList()
//|
for (a <
假设我们有一份清单:
val list: MutableList<out X> = mutableListOf(...)
val list2 = list as MutableList<X>
因此,我们有一个警告Unchecked cast: MutableList<out X> to MutableList<X>
为什么这个演员在这种情况下是危险的?
我不知道如何在Kotlin中释放对象的非空变量。下面的代码是TextView列表。它被初始化为一个非空对象。
class aaa{
var DenomList: MutableList<TextView> = mutableListOf()
var DenomPCSList: MutableList<TextView> = mutableListOf()
var DenomAmountList: MutableList<TextView> = mutableListOf()
var DenomNotationList: Mut
我的代码类似于:
fun verify(problem,answer){
val errors: MutableList<String> = mutableListOf()
if (!condiditon_1) {
errors.add("Condition_1 is not verified")
}
if (!condition_2) {
errors.add("Condition_2 is not verified")
}
return errors
}
这里,err
下面的代码是我将RxJava示例转换为Kotlin的尝试。它应该把一堆Int收集成一个MutableList,但是我得到了很多错误。
val all: Single<MutableList<Int>> = Observable
.range(10, 20)
.collectInto(::MutableList, MutableList::add)
错误:
Error:(113, 36) Kotlin: Type inference failed: Not enough information to infer parameter
我的界面看起来是这样的:
interface MyObject {
val id: String
val media: MutableList<Uri>
}
我想实现它,其中媒体列表是由MutableList<String>而不是MutableList<Uri>支持的(我需要的原因在我的长问题中已经解释过了)。最简单的方法是:
data class MyObjectPojo(
override val id: String,
val mediaStringList: MutableList<String>
) :
我使用MutableList将python列表存储在SQL数组中,如所描述的
class MyMutableList(MutableList):
@classmethod
def coerce(cls, key, value):
"""Convert plain lists to MutableList."""
if not isinstance(value, MutableList):
if isinstance(value, list):
在ViewModel中,我有MutableLiveData<MutableList<object>>,我想要创建MutableLiveData<MutableList<object>>,但是我不知道如何从MutableList创建List。
以下是我要做的事:
class AppViewModel : ViewModel() {
private val _tasks = MutableLiveData<MutableList<Task>>(mutableListOf())
val tasks: LiveD
我有以下代码,我需要在视图中显示列表的元素,然后从列表中删除这些元素。我一直在研究kotlin中的filter vs map,但没有找到解决方案。
var mutableList: MutableList<Object> = myImmutableList.toMutableList()
for (x in mutableList.indices)
{
val tile = row!!.getChildAt(x % 4)
val label = tile.findViewById(android.R.id.text1) as TextView
label.t
我想声明mutableListOf数组,但我不知道如何声明。谷歌向我展示了一些例子,比如var mutableList1 = mutableListOf()等,但没有给出数组的例子()。
import java.util.*
fun main(args: Array<String>) {
var mutableList1 = mutableListOf<Arrays>()
var mutableList2 = mutableListOf(arrayOf<Int>()) //works, but it contains empty ar
编译中提到的此类代码时出现的Kotlin错误。实际上怎么了?
class Foo{
fun fx(parm: MutableList<Double>) {}
}
val foo = Foo()
val parm: MutableList<Double> = mutableListOf()
every { foo.fx(capture(parm)) }
以下错误
Kotlin: Type mismatch: inferred type is Double but MutableList<Double> was expected
他进行了以下编译:
import scala.collection.mutable.MutableList
abstract class GeomBase[T <: DTypes]
{
val grids = new MutableList[GridBase]
def hexs: MutableList[GridBase#HexG with T#HexTr] = grids.flatMap(_.hexs.toList)
//the above compiles fine
abstract class GridBase
{
val hexs: Mut
我想知道如何创建一个与给定的newmatrix = MutableList<MutableList<Int>>大小相同的matrix = MutableList<MutableList<Boolean>>。特别是,我希望newmatrix是零,我可以通过循环来实现这一点。
第一个想法是这样做:
var newmatrix = matrix
// tworzymy macierz równą zero
for (k in 0..matrix.indices.last) {
for (l in 0..matrix[0].indices.las
我想通过添加一些线程安全特性来实现kotlin的MutableList接口,如果我想实现MutableList接口,并且只覆盖对线程不安全的东西,那么我就选择委托。
每次使用List.toMutableList(),它都返回一个新的MutableList实例,其中包含数据中的所有项。
class ThreadSafeList<E> constructor(data: List<E>): MutableList<E> by data.toMutableList() //different reference (new object created when u
我用的是我的应用柄。我有一个用于自定义用法的BasePresenter,它以一个列表作为参数
abstract class BasePresenter(private val rules :MutableList<Rule>){...}
所有具体的实现演示程序都应该扩展该实现,并提供如下列表
class FirstPresenter(rules: MutableList<Rule>) :BasePresenter<IrisView> (rules) {...}
class SecondPresenter(rules: MutableList<Rule
我试图用字符串填充下面的列表,但我不知道如何设置变量strings以匹配所需的类型。我一直在搞类型错配。(类型不匹配:推断类型为MutableList,但需要字符串)
fun main() {
val inputList: MutableList<MutableList<String>> = mutableListOf()
val n = readLine()!!.toInt()
var strings: String
for (i in 0 until n) {
我基本上想问两个特定的类型-- MutableList和Array。
为什么这是可以的:
var anyList: List<Any> = mutableListOf<String>("1", "2") //why is MutableList covariant?
var intList: List<Int> = mutableListOf<Int>(1, 2)
anyList = intList //Why is this Ok? Where's covariance in the correspond
我有一个家庭作业问题:
编写编译良好的代码,但在将MutableList<类型的值赋给"MutableList < Int >“类型的变量后抛出异常。
这就是我的看法:在cast(get)中抛出null,如果我们将< Int?>改为< Int >,这可以用于MutableList,我们有null异常。我不能理解这个任务。InteliJ IDEA每次都会停止运行此编译。