Write a Scala program to reverse a given list.
object Scala_List { def main(args: Array[String]): Unit = { val nums = List(1,2,3,4,5,6,7,8,9,10) println("Original List") println(nums) println("Reversed the said list:") println("Using reverse() function:") println(nums.reverse) println("Using for loop:") for(n<-nums.reverse) { print(n) print(" ") } } }
Sample Output:
Original List List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) Reversed the said list: Using reverse() function: List(10, 9, 8, 7, 6, 5, 4, 3, 2, 1) Using for loop: 10 9 8 7 6 5 4 3 2 1
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer