user12595983
user12595983

Reputation: 147

access variable inside an array [Kotlin]

It is a program using array to hold running paths. fun printMaxMinPath() is to print out the longest and shortest paths. My questions are:

  1. how can i get access to the length of Runningpath and find the max and min? if i write the code as below, line 19, 20 won't work as "length" is unsolved reference. but i don't know how should i write, (or what keywords to google).

  2. how can i get access to the name and spot of longest and shortest Runningpath to print them inside printMaxMinPath()? $min.name won't work

Any hints is very appreciated. Thank you for the kindness.

my codes with compile errors are as below:

 class  Runningpath(val name: String, val length: Int, val spot: String){                              
            override fun toString(): String= "The path $name ($length m) is near $spot"                       
                                                                                                                                                                                                       
            fun printMaxMinPath (p: Array<Runningpath> , min:Int, max:Int) {                                  
                println("The shortest path is with $min m. It is the path $name near $spot")                  
                println("The longest path is with $max m. It is the path $name near $spot")                   
                                                                                                              
            }                                                                                                 
        }                 
        
        fun main() {                                                                                          
            println("Warming up")                                                                             
            val input1 = Runningpath("in Forest", 2000, "some houses")                                        
            val input2 = Runningpath("at lake", 1500, "a school")                                             
            val input3 = Runningpath("in mountain", 2800, "under sky")                                        
            val path = arrayOf(input1, input2, input3)                                                        
                                                                                
    line19   val max = path.length.max()                                                                       
        20   val min = path.length.min()                                                                       
            input1.printMaxMinPath(path, min, max)                                                            
        }                                                                                                     
                                             

                                                             
                                      

                     

                         

Upvotes: 0

Views: 149

Answers (1)

Animesh Sahu
Animesh Sahu

Reputation: 8106

You can use maxBy and minBy from the standard kotlin library to get object having maximum and minimum length, and then use their properties to print their values out.

data class Runningpath(val name: String, val length: Int, val spot: String)

fun main() {
    println("Warming up")
    val input1 = Runningpath("in Forest", 2000, "some houses")
    val input2 = Runningpath("at lake", 1500, "a school")
    val input3 = Runningpath("in mountain", 2800, "under sky")
    val paths = arrayOf(input1, input2, input3)

    printMaxMinPath(paths)
}

fun printMaxMinPath(paths: Array<RunningPath>) {
    val max = path.maxBy { it.length }
    val min = path.minBy { it.length }
    println("The shortest path is with ${min.length} m. It is the path ${min.name} near ${min.spot}")
    println("The longest path is with ${max.length} m. It is the path ${max.name} near ${max.spot}")
}

Edit: The minBy and maxBy may return null if the array provided was null. You have to safe assert them, you can do it before printing anything as follows.

fun printMaxMinPath(paths: Array<RunningPath>) {
    val max = path.maxBy { it.length }
    val min = path.minBy { it.length }
    min?.run { println("The shortest path is with $length m. It is the path $name near $spot") }
    max?.run { println("The longest path is with $length m. It is the path $name near $spot") }
}

Upvotes: 1

Related Questions