WebSep 30, 2024 · scala> val a = Array (1, 2, 3, 4, 5) a: Array [Int] = Array (1, 2, 3, 4, 5) scala> for (e <- a) yield e res5: Array [Int] = Array (1, 2, 3, 4, 5) scala> for (e <- a) yield e * 2 res6: Array [Int] = Array (2, 4, 6, 8, 10) scala> for (e <- a) yield e % 2 res7: Array [Int] = Array (1, 0, 1, 0, 1) WebApr 17, 2024 · import scala.collection.immutable._ // Creating object object GFG { // Main method def main (args:Array [String]) { // Creating and initializing immutable lists val mylist: List [String] = List ("Geeks", "For", "geeks", "is", "a", "fabulous", "portal") // Display the value of mylist in // reverse order using for loop for(element<-mylist.reverse) {
How to reverse a list in scala - GeeksforGeeks
WebOct 13, 2024 · This means it can be used in most common standard collections, such as List, Set, Array, and so on: scala> Array ( "a", "b", "c" ).mkString ( "/" ) val res4: String = a/b/c scala> Set ( "a", "b", "c" ).mkString ( … WebExample #1 – Basic for loop Syntax: for( item <- List){ // Inner loop code } In the syntax, we are iterating over all the elements of a list. For each iteration value of one of the elements is sequentially placed in the item var. We can then use this item var for operating on it. Code: Print values of items in a list 90播王明军
How to iterate over Scala Maps (for, foreach loop, and printing ...
WebHow to Process an Array in Scala? Since we know the type of elements and the size of the Scala array, we can use loop control structures to process an array. Let’s take an example of processing Scala Array. To iterate over the array: scala> var a=Array(1,2,3) a: Array[Int] = Array(1, 2, 3) scala> for(i<-a) { println(i) } 1 2 3 WebJul 8, 2014 · In Scala arrays are immutable objects. You create an array like this: var myArray : Array [String] = new Array [String] (10); First you declare variable var myArray to … WebIn Scala these collection classes are preferred over Array. (More on this later.) The foreach method. For the purpose of iterating over a collection of elements and printing its contents you can also use the foreach method that’s available to Scala collections classes. 90散件升级