Câu hỏi phỏng vấn Kotlin
Câu hỏi

How to initialize an array in Kotlin with values?

Câu trả lời

Here are a few ways to initialize an array with values in Kotlin:

Using the arrayOf() function

kotlin Copy
val numbers = arrayOf(1, 2, 3, 4, 5)

This creates an array of integers with the specified values.

Using the Array constructor

kotlin Copy
val numbers = Array(5) { i -> (i + 1) * 10 }

This creates an array of size 5 and initializes each element using a lambda expression. The lambda takes the index i and returns (i + 1) * 10, which will result in the values ``.

Initializing an array with a specific type

kotlin Copy
val numbers = intArrayOf(1, 2, 3, 4, 5)

This creates an array of primitive integers. Kotlin provides specialized array types like `intArrayO...

junior

junior

Gợi ý câu hỏi phỏng vấn

expert

When to use and do not use an inline function in Kotlin?

expert

What is a motivation to make classes final by default in Kotlin? Do you agree with that decision?

expert

Why do we use companion object as a kind of replacement for Java static fields in Kotlin?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào