https://kotlinlang.org logo
#announcements
Title
# announcements
a

Ashutosh Panda

12/04/2019, 10:01 AM
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

gian

12/04/2019, 10:05 AM
Copy code
arrayOfNulls<YourType>(10)
2
😄 1
a

Ashutosh Panda

12/04/2019, 10:05 AM
Thanks
g

gian

12/04/2019, 10:06 AM
j

jimn

12/04/2019, 10:06 AM
as it happens this must be a nullable type, right?
g

gian

12/04/2019, 10:06 AM
Yes, otherwise you need to initialize each value
a

Ashutosh Panda

12/04/2019, 10:07 AM
so now if I entered first entry will it be null?
g

gian

12/04/2019, 10:07 AM
Copy code
Array(10) { i -> YourType() }
d

diesieben07

12/04/2019, 10:07 AM
YourType
doesn't have to be nullable, but the resulting array will be
Array<YourType?>
2
So the elements can be null
a

Ashutosh Panda

12/04/2019, 10:07 AM
Type is a class
so will it work
j

jimn

12/04/2019, 10:08 AM
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