w3resource

Check a given list is empty or not


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

Go to:


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

Scala Code Editor :

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

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.