This should be, in my opinion, something that should be in the official Kotlin documentation as one of the examples. However, this is not the case. I think that is the wrong approach, especially since a lot of Kotlin developers are people who are switching from Java, where it is very common to make an empty array of a specified size. Consider the following code in Java:
int[] arr = new int[6]; // creates and initializes an integer array with 6 elements (automatically set to 0)
Object[] arr = new Object[6]; // creates and initializes an object array with 6 elements (elements with null values)
String[] arr = new String[6]; // an array of strings that are null (which makes sense since strings are objects that have to be initialized individually, like each of the elements of any other array of objects)
Compare this, however, to Kotlin, and there is a slightly different story. In some ways, it is very straight forward and easy in Kotlin to create arrays. Consider:
val arr = arrayOf() // creates and initializes an array of zero elements
val arr = arrayOf<Int>() // creates and initializes an array of integers of zero elements
One might think that the parentheses in arrayOf could be used to specify the number of elements, and you’d be forgiven for thinking that is all you need to do, since if you hover over arrayOf<Int>(5) in Android Studio, it would say that a size is meant to be specified there, but if you wrote:
val arr = arrayOf<Int>(5) // does not compile
You would find that it would not work and you’d get an error that tells you that a value for init needs to be entered. Init is not optional here. For new Kotlin developers, this could be very confusing. Init, in this case, actually refers to the init keyword used for init blocks. An init block in kotlin allows you to do various initialization tasks for a class, upon its initialization, that you can’t do in the primary constructor (since the primary constructor only allows for the assignment of properties). For further reading on init, read about it here.
So how do we do what we need to do if we wanted to create an array of strings of a specified size but with a default value for each element?
val arr = arrayOfNulls<String>(6) // creates and initializes an array of 6 nullable strings, each with a value of NULL. You could use this as a workaround in certain situations
val array : Array<String?> = Array<String?>(6) { null } // an array of nullable strings where each element is equal to NULL.
val array : Array<String> = Array<String>(6) { "" } // creates and initializes an array of 6 strings, then running an operation to set each element equal to an empty string
val arr2 = Array(6) { 0 } // also works
val arr3 = Array<Int>(6) { ((Math.random() * 10) +1).toInt() } // creates an integer array of six elements, each element equaling a random number between 1 and 10.
The approach with arrayOfNulls is actually extremely similar to the Java approach to creating an array of objects (such as strings). Kotlin does not go by the Java approach to nullability, however, so keep in mind that by default variables when you create them are not nullable and it is only through functions like arrayOfNulls or specifying with a question mark (?) that variables can be nullable. There is less guesswork in Kotlin.
Kotlin is a bit quirky and not exactly obvious in some ways to Java developers, but it also has its own merits. We should respect the differences between Java and Kotlin. I hope this article is helpful for you new Kotlin developers out there.