How to pop the first element of a list in Kotlin

1 Answer

0 votes
fun main() {
    // Initialize a list equivalent to std::vector in C++
    var list = listOf(1, 2, 3, 4, 5)

    // Check if the list is not empty
    if (list.isNotEmpty()) {
        // Remove the first element
        list = list.drop(1)
    }

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


   
      
/*
run:

2 3 4 5
  
*/

 



answered May 3, 2025 by avibootz
...