How to print a list in Scala
In Scala, list is defined under scala.collection.immutable
package. A list is a collection of same type elements which contains immutable data.
There are multiple ways to create a List in Scala. Let’s see few basic on how to create a Scala List.
- Create an empty List
Example :
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
val emptylist : List[Nothing] = List()
println( "The empty list is: " + emptylist)
}
}
|
Output:
The empty list is: List()
- Create a simple list
Example :
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
val mylist : List[String] = List( "Geeks" , "For" , "geeks" )
println( "List is: " + mylist)
}
}
|
Output:
List is: List(Geeks, For, geeks)
- Using for loop to print elements of List
Example :
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
val mylist : List[String] = List( "Geeks" , "For" ,
"geeks" , "is" ,
"a" , "fabulous" ,
"portal" )
for (element < -mylist)
{
println(element)
}
}
}
|
Output:
Geeks
For
geeks
is
a
fabulous
portal
Last Updated :
15 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...