How to create an array in Kotlin specifying only the size, like in Java (new int[10])?
I just started learning Kotlin after Java and ran into a problem. In Java, I was used to creating arrays like this:
```java
int[] numbers = new int[10];
String[] names = new String[5];
```
That is, I just specify the type and size, and the array is filled with default values (0 for int, null for String). This is very convenient when I know in advance how many elements there will be, but don't know their specific values.
In Kotlin, I tried to do something similar:
```kotlin
var numbers: IntArray = IntArray(10) // This works, but I'm not sure it's correct
var names: Array = Array(5) // And this gives a compilation error
```
I see that `IntArray(10)` seems to create an array of 10 zeros, but the syntax with `Array(5)` doesn't work. In the documentation, I found that you need to use l
No answers yet.
Log in to leave an answer
Blogs