
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - toList() Function
The Kotlin array toList() function is used to convert an array into a list. This function creates a new list instance containing all elements of an array in the same order.
Syntax
Following is the syntax of Kotlin array toList() function −
fun <T> Array<out T>.toList(): List<T>
Parameters
This function does not accepts any parameter.
Return value
This function returns a list containing all elements of an array.
Example 1
Following is a basic example to demonstrate the use of toList() function to return a list −
fun main(args: Array<String>){ var arr = arrayOf(3, 4, 5, 6) print("Array: ") println(arr.joinToString()) // use toList function to convert an array val list = arr.toList() println("list: "+ list) }
Output
The above code generate following output −
Array: 3, 4, 5, 6 list: [3, 4, 5, 6]
Example 2
Let's see another example. Here, we use the toList function to return a list containing elements of an array in same order −
fun main() { // array of integers val intArray = arrayOf(1, 2, 3, 4, 5) // Convert the array to a list val intList: List<Int> = intArray.toList() // display the list println(intList) }
Output
Following is the output −
[1, 2, 3, 4, 5]
Example 3
The below example converts an array into a list to perform list operations. We then display the student's marks and calculate the average score −
fun main(args: Array<String>) { val studentScores = arrayOf(45, 78, 88, 56, 90, 67, 33, 82, 58, 94) // Convert the array to a list val scoreList: List<Int> = studentScores.toList() val passingScore = 60 // Filter the list to get scores of students who passed val passingStudents = scoreList.filter { it >= passingScore } println("Students who passed: $passingStudents") // Calculate the average score of passing students val averagePassingScore = if (passingStudents.isNotEmpty()) { passingStudents.average() } else { 0.0 } println("Average score of passing students: $averagePassingScore") }
Output
The above code produce following output −
Students who passed: [78, 88, 90, 67, 82, 94] Average score of passing students: 83.16666666666667