English 中文(简体)
Scala Collections - drop
  • 时间:2024-12-22

Scala Collections - Drop Method


Previous Page Next Page  

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

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 pst. This method returns the all the elements of pst 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 pst = List(1, 2, 3, 4, 5)
      // print pst
      println(pst)
      //apply operation
      val result = pst.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