List Methods
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 70 of 93.
Beyond adding and removing elements, lists come with helpful methods that let you query and transform their contents. These work on both List and MutableList.
To check if a list has any elements, use isEmpty() or isNotEmpty():
val tasks = listOf("Email", "Call")
println(tasks.isEmpty()) // false
println(tasks.isNotEmpty()) // trueFor numeric lists, you can quickly calculate common statistics:
val scores = listOf(85, 92, 78, 95)
println(scores.sum()) // 350
println(scores.average()) // 87.5
println(scores.max()) // 95
println(scores.min()) // 78To find an element's position, use indexOf(). It returns the index of the first occurrence, or -1 if the element isn't found:
val colors = listOf("Red", "Green", "Blue", "Green")
println(colors.indexOf("Green")) // 1
println(colors.indexOf("Yellow")) // -1You can also reverse a list or sort it:
val numbers = listOf(3, 1, 4, 1, 5)
println(numbers.reversed()) // [5, 1, 4, 1, 3]
println(numbers.sorted()) // [1, 1, 3, 4, 5]These methods return new lists and don't modify the original, making them safe to use anywhere.
Challenge
MediumYou are provided with the following list:
val temperatures = listOf(72, 85, 90, 68, 75, 88, 92, 70)Using list methods, print the following information on separate lines in this exact order:
- The sum of all temperatures
- The average temperature
- The highest temperature
- The lowest temperature
- The index of
90in the list - The list sorted in ascending order
Try it yourself
fun main() {
val temperatures = listOf(72, 85, 90, 68, 75, 88, 92, 70)
// TODO: Write your code below
// Use list methods to calculate and print:
// 1. The sum of all temperatures
// 2. The average temperature
// 3. The highest temperature
// 4. The lowest temperature
// 5. The index of 90 in the list
// 6. The list sorted in ascending order
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic InputPractice on your own: Kotlin playground