How to shuffle the input list and output it in Kotlin?
In Kotlin, you can use the shuffle() function to randomize the order of elements in a list, and then use the forEach() function to output the shuffled elements. Here is an example code:
fun main() {
val list = listOf(1, 2, 3, 4, 5)
val shuffledList = list.shuffled()
shuffledList.forEach { element ->
println(element)
}
}
When you run the code, you will get output similar to the following, but the order will be random.
3
1
5
4
2
In this example, we start by defining a list containing integers. Then, we use the shuffled() function to shuffle the order of the list elements and assign the result to shuffledList. Finally, we use the forEach() function to iterate through each element in shuffledList and print it out.