1

In Java an array can be initialized such as:

public static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

How does Kotlin's array initialization look like?

2

3 Answers 3

1

You can initialize using

val MONTHS = arrayof("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Sign up to request clarification or add additional context in comments.

Comments

0

Easy:

val MONTHS = arrayOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

Comments

0

To create an array, we can use a library function arrayOf() and pass the item values to it, so that arrayOf("1", "2", "3").

You can use:

val MONTHS = arrayOf("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

Also you can use specialized classes to represent arrays of primitive types as ByteArray, ShortArray, IntArray.

In this case you can use:

val x: IntArray = intArrayOf(1, 2, 3)

More detailed info here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.