Scala - How can I use map and receive an index as well? Code Example

The code for How can I use map and receive an index as well?

object MyClass {
    def main(args: Array[String]) {
        val cities = List("New Delhi", "Mumbai", "Chennai", "Bhopal")
    
        val result = cities.zipWithIndex.foreach{
            case (ele, ind) => println(ind + " : "+ ele)
        }
        
        println(result)
    }
}

/*
Output:
0 : New Delhi
1 : Mumbai
2 : Chennai
3 : Bhopal
()
*/
Code by IncludeHelp, on August 15, 2022 23:10

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.