Junior — Middle
Kas on võimalik deklareerida muutuja val (first, second), kasutades pair objekti, kus pair on klass Pair<String, String>?
sobes.tech AI
Vastus AI-lt
In Kotlin (the main language for Android) you can declare a variable with object destructuring if the class supports componentN functions.
The class Pair<String, String> already implements these functions, so you can write:
val pair = Pair("firstValue", "secondValue")
val (first, second) = pair
println(first) // firstValue
println(second) // secondValue
Thus, the declaration val (first, second) = pair is possible and correct if pair is an object of class Pair<String, String>. This is a convenient way to immediately access the elements of the pair.