Scala Sequence

Sequence is an iterable collection of class Iterable. It is used to represent indexed sequences that are having a defined order of element i.e. guaranteed immutable. The elements of sequences can be accessed using their indexes. Method apply is used for the purpose of indexing. Sequences can also be accessed reversibly using the method reverse and reverseIterator. 
The indices range from 0 to (n – 1) Where, n= the length of the sequence. For the purpose of finding the subsequences, sequences support various methods. Methods such as indexOf, segmentLength, prefixLength, lastIndexWhere, lastIndexOf, startsWith, endsWith. There are two primary subtraits of Sequence namely IndexedSeq and LinearSeq which gives different performance guarantees. IndexexedSeq provides fast and random access of elements while LinearSeq provides fast access to the first element only via head and also contains a fast tail operation. 
Example #1:

// Scala program to illustrate sequence

import scala.collection.immutable._

object GFG

{

    // Main Method

    def main(args:Array[String])

    {

        // Initializing sequence

        var seq:Seq[Int] = Seq(1,2,3,4,5,6)

         

        // Printing Sequence

        seq.foreach((element:Int) => print(element+","))

        println("\nElements Access Using Index")

        println(seq(0))

        println(seq(1))

        println(seq(2))

        println(seq(3))

        println(seq(4))

        println(seq(5))

    }

}

Output: 

1,2,3,4,5,6,
Elements Access Using Index
1
2
3
4
5
6

Some of the Predefined Methods used in Sequence 
 

  • def apply(index: Int): A -> To select an element from the sequence
  • def contains[A1 >: A](elem: A1): Boolean -> To check whether a sequence contains the given element
  • def count(p: (A)=> Boolean): Int-> To count the number of elements that satisfies a predicate
  • def length: Int -> gives the length of the Sequence
  • def copyToArray(xs: Array[A], start: Int, len: Int): Unit -> For copying the elements of Sequence to array
  • def endsWith[B](that: GenSeq[B]): Boolean-> to check whether a sequence terminates with a given sequence or not
  • def head: A ->It selects the first element of the sequence.
  • def indexOf(elem: A): Int-> To find the index of first occurrence of a value in the sequence
  • def isEmpty: Boolean ->To test the emptiness of the sequence.
  • def lastIndexOf(elem: A): Int-> To find the index of last occurrence of a value in the sequence
  • def reverse: Seq[A]-> To return a new sequence with elements in reverse order.

Sequence Example using Predefined methods 
Example #2:

// Scala program to illustrate sequence

object MainObject

{

    // Main Method

    def main(args:Array[String])

    {

        // Initializing sequence

        var seq:Seq[Int] = Seq(1, 2, 3, 4, 5, 6)

         

        // Printing Sequence

        seq.foreach((element:Int) => print(element+","))

         

        // Using Some Predefined Methods

        println("\nis Empty: "+ seq.isEmpty)

        println("\nEnds with (5,6): "+ seq.endsWith(Seq(5,6)))

        println("\nLength of sequence: "+ seq.length)

        println("\ncontains 3: "+ seq.contains(3))

        println("\nlast index of 4 : "+ seq.lastIndexOf(4))

        println("\nReversed sequence: "+ seq.reverse)

    }

}

Output: 

1,2,3,4,5,6,
is Empty: false

Ends with (5,6): true

Length of sequence: 6

contains 3: true

last index of 4 : 3

Reversed sequence: List(6, 5, 4, 3, 2, 1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值