How to skip only first element of List in Scala
Last Updated :
17 Apr, 2019
In Scala, list is defined under scala.collection.immutable
package. A list is a collection of same type elements which contains immutable data. Let’s see how to print the element of given List, by skipping the first item in Scala.
List in Scala contains many suitable methods to perform simple operations like head(), tail(), isEmpty(). Coming to list, tail() method is used to skip the first element of the list.
below are examples to skip only first element of List.
Example :
import scala.collection.immutable. _
object GFGobject
{
def main(args : Array[String])
{
val mylist : List[String] = List( "Geeks" , "For" , "geeks" , "is" ,
"a" , "fabulous" , "portal" )
println(mylist.tail)
}
}
|
Output:
List(For, geeks, is, a, fabulous, portal)
Example :
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
val myList = List.range( 10 , 19 )
println(myList)
println( "Using tail() method: " + myList.tail)
}
}
|
Output:
List(10, 11, 12, 13, 14, 15, 16, 17, 18)
Using tail() method: List(11, 12, 13, 14, 15, 16, 17, 18)
Share your thoughts in the comments
Please Login to comment...