Scala Collections - Drop Method


Advertisements

drop() method is method used by List to select all elements except first n elements of the list.

Syntax

The following is the syntax of drop method.

def drop(n: Int): List[A]

Here, n is the number of elements to be dropped from the list. This method returns the all the elements of list except first n ones.

Usage

Below is an example program of showing how to use drop method −

Example

object Demo {
   def main(args: Array[String]) = {
      val list = List(1, 2, 3, 4, 5)
      // print list
      println(list)
      //apply operation
      val result = list.drop(3)
      //print result
      println(result)      
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

List(1, 2, 3, 4, 5)
List(4, 5)
Advertisements