I would like to know how to start an array similar to this String [] errorSoon = {"Hello", "World"}; in Kotlin. How is done?
6 Answers
You can use arrayOf() fuction as it described in Kotlin Basic Type article.
Your code will be the next:
val errorSoon = arrayOf("Hello", "World")
Comments
Declare array at global
names = arrayOf(String())
Now in onCreate method Initialize you array with value
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Init array here
names = arrayOf("Friends List", "Notification")
}
1 Comment
Mayank Patel
Please add a short description of how your code answers the question. It would help others understand your answer better.
Try this arrayOf() to create array in Kotlin
val errorSoon = arrayOf("a", "b", "c")
to get values from array use below code
for (i in errorSoon.indices) {
print(errorSoon[i]+" ")
}
You can read more here about it
Comments
array of strings can be initialized with square brackets too.
values = [ "a", "b" ]
1 Comment
Oleg Gryb
not supported in Android's Kotlin: "collection literals are outside of annotations"