Pairs

Pairs represent a generic pair of two values.


// File:            Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/21/2023
// Email: nolvertou@gmail.com
// Description: Using pairs

fun main(args: Array<String>) {
val equipment = "fishnet" to "cashing fish"
println(equipment) // Output: (fishnet, cashing fish)
println(equipment.first) // Output: fishnet
println(equipment.second) // Output: cashing fish

val equipment2 = "fishnet" to "catching fish" to "of a big size" to "and strong"
println(equipment2) // Output: (((fishnet, catching fish), of a big size), and strong)
println(equipment2.first) // Output: ((fishnet, catching fish), of a big size)
println(equipment2.second) // Output: and strong
println(equipment2.first.first) // Output: (fishnet, catching fish)
println(equipment2.first.second) // Output: of a big size

val (tool, use) = equipment
println(equipment) // Output: (fishnet, cashing fish)
println(tool) // Output: fishnet
println(use) // Output: cashing fish
println("The $tool is a tool for $use")

val equipmentString = equipment.toString()
println(equipmentString) // Output: (fishnet, cashing fish)

fun giveMeATool(): Pair<String, String>{
return ("fishnet" to "catching")
}

val (tool2, use2) = giveMeATool()
println(tool2) // fishnet
println(use2) // catching
}

Comments

Popular posts from this blog