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


All Scala Programs

Comments and Discussions!

Load comments ↻





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