How to create an empty array in Kotlin?

Kotlin Mobile DevelopmentApps/Applications

An array is a collection where we can store multiple items of the same type. We can consider an array of integers or an array of strings. This is very useful, for example, whenever we need to store the name of 1000 students in a single variable.

Example – Using arrayOf()

In this example, we will see how we can create an empty array in Kotlin. We will be creating an empty array of Strings and manipulate the same in the program.

fun main(args: Array<String>) {
   // Declareting empty array of type String
   val emptyStringArray = arrayOf<String>()
   println("Example of empty String array")

   // printing the empty array
   println(emptyStringArray.contentToString())

   // Declareting empty array of type Int
   val emptyIntArray = arrayOf<Int>()
   println("Example of empty Int array")

   // printing the empty array
   println(emptyIntArray.contentToString())
}

Output

On execution, it will produce the following output −

Example of empty String array
[]
Example of empty Int array
[]
raja
Updated on 01-Mar-2022 11:01:38

Advertisements