Hallo there
Sorry, if that is the wrong forum for this kind of questions. But I didn’t find another one.
My question is, how can I declare an empty array? I just found the way for create arrays with providing the desired size and the init function.
Thanks in advance
Stefan
The easiest way is this:
``
val a = array<Int>()
You can also say
``
val a = Array<Int>(0, {0})
Thanks for the quick answer! Now I also understand function literals a little bit better.
The web demo mentioned, that with your second example a class has been generated (like namespace$main$a$1.class) while with the first example no class has been generated. Please, could you explain why Array leads to a new class and array() not??
I tried to find the library function array() but with no succcess. Where can I find it’s definition?
Cheers
Stefan
You can simply navigate to a function definition by Ctrl+Click (Cmd+Click on Mac).
Here’s the source on github: https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/ArraysJVM.kt#L9
The class is created due to lack of an optimization in the current version of the compiler. It won’t be created in the future.
Thanks. Same behaviour like in java files... I should have been trying this :8}
Or
val arr = emptyArray<Int>()