You can use indexOf
times.indexOf(Some("Last month")) // returns 1
Update
You can convert the times ArrayBuffer[Option[String]] into a ArrayBuffer[String] via
val l = times.map(_.getOrElse("")) // ArrayBuffer("Last week", "Last month", ...)
And then you can search for the strings
l.indexOf("Last month")
Answer from Till Rohrmann on Stack OverflowScala Programming Language
scala-lang.org › api › 2.13.3 › scala › collection › mutable › ArrayBuffer.html
Scala Standard Library 2.13.3 - scala.collection.mutable.ArrayBuffer
Get the element at the specified index. This operation is provided for convenience in Seq.
Scala Programming Language
scala-lang.org › api › current › scala › collection › mutable › ArrayBuffer.html
ArrayBuffer
the element to add. Returns · the array buffer itself · Source · ArrayBuffer.scala · def apply(n: Int): A · Gets the element at the specified index. This operation is provided for convenience in Seq. It should not be assumed to be efficient unless you have an IndexedSeq.
Top answer 1 of 3
4
You can use indexOf
times.indexOf(Some("Last month")) // returns 1
Update
You can convert the times ArrayBuffer[Option[String]] into a ArrayBuffer[String] via
val l = times.map(_.getOrElse("")) // ArrayBuffer("Last week", "Last month", ...)
And then you can search for the strings
l.indexOf("Last month")
2 of 3
2
You can use indexOf or lastIndexOf. Example:
times indexOf ( Some( "Last year" ) )
Also you can use indexWhere or lastIndexWhere. For example:
times indexWhere { _.get == "Last year" }
GeeksforGeeks
geeksforgeeks.org › scala › scala-arraybuffer
Scala | ArrayBuffer - GeeksforGeeks
March 5, 2019 - Array in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size can’t change. To create a mutable, indexed sequence whose size can change ArrayBuffer class is used. To use, ArrayBuffer, scala.collection.mutable.ArrayBuffer class is imported, an instance of ArrayBuffer is created.
Alvin Alexander
alvinalexander.com › scala › arraybuffer-class-methods-syntax-examples-reference
Scala ArrayBuffer class: methods, syntax, and examples | alvinalexander.com
As with other Scala sequence classes, you access ArrayBuffer elements by their index, inside parentheses:
Allaboutscala
allaboutscala.com › tutorials › chapter-7-beginner-tutorial-using-scala-mutable-collection › scala-tutorial-learn-use-mutable-arraybuffer
Scala Tutorial - Learn How To Use Scala's Mutable ArrayBuffer
February 3, 2020 - In this Scala beginner tutorial, you will learn how to use Scala's Mutable ArrayBuffer: initialize, access element at specific index, add, remove elements.
TutorialsPoint
tutorialspoint.com › scala_collections › scala_collections_arraybuffer.htm
Scala Collections - ArrayBuffer
In case underlying array is full then a new larger array is created and all elements are copied to larger array. The following is the syntax for declaring an ArrayBuffer variable. ... Here, z is declared as an array-buffer of Strings which is initially empty. Values can be added by using commands like the following − ... import scala.collection.mutable.ArrayBuffer object Demo { def main(args: Array[String]) = { var myList = ArrayBuffer("Zara","Nuha","Ayan") println(myList); // Add an element myList += "Welcome"; // Add two element myList += ("To", "Tutorialspoint"); println(myList); // Remove an element myList -= "Welcome"; // print second element println(myList(1)); } }
Scala-lang
nightly.scala-lang.org › api › scala › collection › mutable › ArrayBuffer.html
ArrayBuffer
Add s a single element to this array buffer. ... Gets the element at the specified index. This operation is provided for convenience in Seq.
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Scala Programming Language
scala-lang.org › api › 2.13.6 › scala › collection › mutable › ArrayBuffer.html
Scala Standard Library 2.13.6 - scala.collection.mutable.ArrayBuffer
Get the element at the specified index. This operation is provided for convenience in Seq.
Scala Programming Language
scala-lang.org › api › 2.13.0 › scala › collection › mutable › AbstractBuffer.html
Scala Standard Library 2.13.0 - scala.collection.mutable.AbstractBuffer
Adds a single element to this growable collection. ... Get the element at the specified index.
Otfried
otfried.org › scala › arraybuffer.html
Scala Documentation -- ArrayBuffer
You can add elements using +=. When you are done, the toArray method returns the contents as a normal array. scala> val A = scala.collection.mutable.ArrayBuffer.empty[Int] A: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> A += 1 res9: A.type = ArrayBuffer(1) scala> for (i ...
Hello-scala
hello-scala.com › 201-scala-arraybuffer-examples.html
hello-scala.com - hello scala المصادر والمعلومات.
hello-scala.com هل هو أول وأفضل مصادر جميع المعلومات التي تبحث عنها. من ضوء الموضوعات العامة إلى مزيد من الموضوعات التي كنت لتتوقعها هنا، hello-scala.com تمتلكها جميعًا.
Scala Programming Language
scala-lang.org › api › 2.13.x › scala › collection › mutable › ArrayBuffer.html
Scala Standard Library 2.13.18 - scala.collection.mutable.ArrayBuffer
February 13, 2011 - a new collection of type CC[B] which contains all elements of this array buffer followed by all elements of suffix. ... Gets the element at the specified index.
GitHub
github.com › scala › docs.scala-lang › blob › main › _overviews › scala-book › arraybuffer-examples.md
docs.scala-lang/_overviews/scala-book/arraybuffer-examples.md at main · scala/docs.scala-lang
You remove elements from an ArrayBuffer with the -= and --= methods: // remove one element nums -= 9 // remove multiple elements nums -= 7 -= 8 // remove multiple elements using another collection nums --= Array(5, 6) Here’s what all of those examples look like in the REPL: scala> import scala.collection.mutable.ArrayBuffer scala> val nums = ArrayBuffer(1, 2, 3) val nums: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> nums += 4 val res0: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4) scala> nums += 5 += 6 val res1: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 6) scala> nums ++= List(7, 8, 9) va
Author: scala