How to pop the first element of a list in Scala

1 Answer

0 votes
object Main extends App {
  // Initialize a List equivalent to std::vector in C++
  var list = List(1, 2, 3, 4, 5)

  // Check if the list is not empty
  if (list.nonEmpty) {
    // Remove the first element
    list = list.tail
  }

  // Print the updated list
  println(list.mkString(" "))
}




/*
run:
   
2 3 4 5
 
*/

 



answered May 3, 2025 by avibootz
...