How to get the first element of list in Scala?
belongs to collection: Scala List Programs
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
belongs to collection: Scala List Programs
total answers (1)
Accessing the first element using the index value,
We can easily access the first element of the list by using its index value. And the indexing of the list is similar to that of an array i.e. the indexing starts at 0.
So, the first element will be at 0 index in the list.
Syntax:
Program to illustrate the working of our solution
Output:
Accessing the first element of the list using the list.head method
There are many inbuilt methods to support the functioning of a data structure in Scala. List is not an exception to that; the first element can be accessed using the inbuilt function head.
Syntax:
Program to illustrate the working of our solution
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer