Home » Scala language

How to get the first element of the list in Scala?

Here, we age going to learn how to get the first element of the list in Scala programming language?
Submitted by Shivang Yadav, on April 21, 2020

List

A list is a linear data structure. It is a collection of elements of the same data types.

Scala libraries have many functions to support the functioning of lists. Methods like isempty, head, tail, etc provide basic operations on list elements.

Getting first element of the list

You can access the first element of the list in Scala using the head method form the list.

Program:

object MyObject{ 
	def main(args:Array[String]) { 
		val myBikes = List("ThunderBird 350", "Yamaha R3", "BMW S1000R", "Iron 883")
		println("list of my Bikes : " + myBikes) 
		println("First Bike from the list is: " + myBikes.head) 
	} 
} 

Output

list of my Bikes : List(ThunderBird 350, Yamaha R3, BMW S1000R, Iron 883)
First Bike from the list is: ThunderBird 350





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.