- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 []
Advertisements