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!





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.