Q:

Write a Scala program to find the first and last element of given list

0

Write a Scala program to find the first and last element of given list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object Scala_List
{
  def main(args: Array[String]): Unit = 
 {
   val colors = List("Red", "Blue", " Black ", "Green", " White", "Pink")
   println("Original list:")
   println(colors)  
   println("First element of the said list: " + colors.head) 
   println("Last element of the said list: " + colors.last)    
  }
}

Sample Output:

Original list:
List(Red, Blue,  Black , Green,  White, Pink)
First element of the said list: Red
Last element of the said list: Pink

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now