I want to create an array in which would contain o...
# announcements
a
I want to create an array in which would contain objects of an class and with size 10 but first I want to declare it globally without any entries in array and later on use a function to entry it. Is it possible in kotlin and what is the syntax
g
Copy code
arrayOfNulls<YourType>(10)
2
😄 1
a
Thanks
g
j
as it happens this must be a nullable type, right?
g
Yes, otherwise you need to initialize each value
a
so now if I entered first entry will it be null?
g
Copy code
Array(10) { i -> YourType() }
d
YourType
doesn't have to be nullable, but the resulting array will be
Array<YourType?>
2
So the elements can be null
a
Type is a class
so will it work
j
this makes it impossible to get Array<T>(10) from an empty array but it seems easier sometimes to do something.map{}.toTypedArray()
it will convert an arraylist for you without T? down the line