w3resource

Check a given list is empty or not

Scala Programming List Exercise-8 with Solution

Write a Scala program to check a given list is empty or not.

Sample Solution:

Scala Code:

object Scala_List
{
def main(args: Array[String]): Unit = 
 {
   val nums = List(1, 3, 5, 2, 7, 9, 11, 5, 2, 14, 12, 3)
   println("Original list:")
   println(nums)   
   val result = nums.isEmpty
   println("Test the said list is empty or not?") 
   println(result)
   val nums1 = List()
   val result1 = nums1.isEmpty
   println("Test the said list is empty or not?") 
   println(result1)
  }
}

Sample Output:

Original list:
List(1, 3, 5, 2, 7, 9, 11, 5, 2, 14, 12, 3)
Test the said list is empty or not?
false
Test the said list is empty or not?
true

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to remove duplicates from a given list.
Next: Write a Scala program to get the difference between two given lists.

What is the difficulty level of this exercise?