How can I tell Kotlin that an array or collection cannot contain nulls?

前端 未结 3 1195
野趣味
野趣味 2021-01-20 15:03

If I create an array, then fill it, Kotlin believes that there may be nulls in the array, and forces me to account for this

val strings = arrayOfNulls

        
3条回答
  •  终归单人心
    2021-01-20 15:39

    There is no way to tell this to the compiler. The type of the variable is determined when it is declared. In this case, the variable is declared as an array that can contain nulls.

    The fill() method does not declare a new variable, it only modifies the contents of an existing one, so it cannot cause the variable type to change.

提交回复
热议问题