How to create an empty array in Kotlin?


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
[]

Updated on: 01-Mar-2022

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements