简体   繁体   中英

How can I declare null array in Kotlin?

I have the following code in PHP public static $bitValueTable = null; and I want to convert it into Kotlin. My variable is a null array in the first step but I add some value after the program runs. how can I convert?

By default any variable in kotlin can't hold null values but still you can create nullable object using ? operator, for better understanding check below url
https://kotlinlang.org/docs/reference/null-safety.html .
So to create nullable array use below syntax

var myTypeArray: Array<type>? = null // check below example
var myStrArray: Array<String>? = null

Kotlin Arrays Documentation

Thanks

In kotlin you can use nullable objects with the secure call operator "?". Now, you have a static variable in PHP, in kotlin there is no "static" as such, however the companion object {} fulfills the same function. In this way the equivalent of public static $bitValueTable = null; in kotlin is: companion object { var bitValueTable : Array<Type>? = null } companion object { var bitValueTable : Array<Type>? = null }

Obs:

  • The default value of the variables in kotlin is public

简单的方法

         val NameList =Array<String?>(5,{null})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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