如何在Kotlin中为ArrayList赋值?

I am trying to add null value to my custom ArrayList like that

var mList = ArrayList<CustomClass>()
mList.add(null)

在java中这是可能的,但我如何在Kotlin中实现这一点?

null cannot be value of non null type kotlin

I need to insert null value for doing load more functionality in RecyclerView

推荐答案

然后你必须这样写.

var mList = ArrayList<CustomClass?>()
mList.add(null)

You have to add a ? after the type in ArrayList because ? allows us to put a null value.

Kotlin相关问答推荐

Fleet无法从构建文件自动重新加载更改错误'

如何访问方法引用的接收者?

用A*搜索算法解决特修斯和米诺陶尔难题

如何检测一个值是否是Kotlin中的枚举实例?

为何Kotlin标准库中的AND和OR函数不能像&&和||一样进行短路运算?

如何缩短 MaterialTheme.colors.primary?

验证构造函数中的值组合

在 Kotlin 中,我可以在集合上有一个条件构建器元素吗?

有没有一种简单的方法可以将数组/列表中的每个元素相互相乘 - Kotlin?

Kotlin:如何使用第一个参数的默认值进行函数调用并为第二个参数传递一个值?

Kotlin 有垃圾收集器吗?如果是这样,它基于哪种算法?

具有多个不同类型来源的 LiveData

如何使用 Kotlin Coroutines 使 setOnClickListener debounce 1 秒?

无法为 retrofit2.Call 调用无参数构造函数

@uncheckedVariance 在 Kotlin 中?

Kotlin - 来自 KType 的 KClass<*>

Kotlin val difference getter override vs assignment

Spring Boot:更改属性占位符符号

Mocked suspend函数在Mockito中返回null

Android Jetpack Compose 中的文本渐变