Welcome to this course! Our goal is to strengthen your understanding of Kotlin by revisiting its basics and preparing you for interviews. In this lesson, we will explore collections—specifically, the types of lists in Kotlin, namely mutable and immutable lists, as well as how to work effectively with strings.
Kotlin offers two flavors of lists, each serving different needs:
- Immutable Lists: Lists where the elements cannot be changed after their creation.
- Mutable Lists: Lists where you can change, add, or remove elements freely.
Here's how you can use immutable lists in Kotlin:
Kotlin1fun main() { 2 // Creating an Immutable List 3 val list = listOf("apple", "banana", "cherry") 4 5 // Accessing elements using indexing 6 val firstElement = list[0] // 'apple' 7 val lastElement = list[list.size - 1] // 'cherry' 8 9 // Getting the size of the list 10 val size = list.size // 3 11 12 // Printing the list 13 println(list) // prints [apple, banana, cherry] 14}
Immutable lists are perfect for use cases where you don't need to modify the contents of the list after its creation. You can find more information in the Kotlin documentation for Immutable Lists.
If you need to modify your list, mutable lists are the way to go. Mutable lists support all the operations available for immutable lists, with additional methods for modification:
Kotlin1fun main() { 2 // Creating a Mutable List 3 val mutableList = mutableListOf("apple", "banana", "cherry") 4 5 // Adding elements 6 mutableList.add("date") // adds 'date' 7 8 // Removing elements 9 mutableList.remove("banana") // removes 'banana' 10 11 // Accessing sublist 12 val sublist = mutableList.subList(1, mutableList.size) // ["cherry", "date"] 13 14 // Finding the index of an element 15 val index = mutableList.indexOf("cherry") // 1 16 17 // Sorting the list 18 val sortedList = mutableList.sorted() // ["apple", "cherry", "date"] 19 20 // Printing the list 21 println(mutableList) // prints [apple, cherry, date] 22}
Mutable lists provide flexibility by allowing modification of the list elements even after its creation. Explore more about them in the Kotlin documentation for Mutable Lists.
Kotlin makes it simple to convert between mutable and immutable lists:
Kotlin1fun main() { 2 // Converting MutableList to List (immutable) 3 val mutableList = mutableListOf("apple", "banana") 4 val immutableList = mutableList.toList() 5 6 // Converting List (immutable) to MutableList 7 val list = listOf("cherry", "date") 8 val newMutableList = list.toMutableList() 9 10 println(immutableList) // Prints [apple, banana] 11 println(newMutableList) // Prints [cherry, date] 12}
These conversions are helpful in situations where you need a list to be mutable or immutable at different stages in your application.
Strings in Kotlin are easy to manipulate:
Kotlin1fun main() { 2 // Creating a string literal 3 val greeting = "Hello, Kotlin!" 4 5 // Using a string template 6 val name = "John" 7 val welcomeMessage = "Hello, $name!" 8 9 // Accessing a character at a specific index 10 val firstChar = greeting[0] // 'H' 11 12 // Slicing: substring 13 val subString = greeting.substring(7, 13) // "Kotlin" 14 15 // Finding the index of a character 16 val indexOfChar = greeting.indexOf('K') // 7 17 // If there are multiple 'K's, indexOf will return the first occurrence. 18 19 // String operations 20 val lowerCase = greeting.lowercase() // "hello, kotlin!" 21 val upperCase = greeting.toUpperCase() // "HELLO, KOTLIN!" 22 23 // Printing manipulated strings 24 println(welcomeMessage) // Prints "Hello, John!" 25 println(lowerCase) // Prints "hello, kotlin!" 26 println(upperCase) // Prints "HELLO, KOTLIN!" 27}
These commands demonstrate how to leverage Kotlin's abilities for efficient string handling and manipulation. Substrings and searching for indexes equip you with tools to precisely manage string data. For additional details, refer to the Kotlin documentation for Strings.
In this lesson, you learned about the key differences between immutable and mutable lists, operations such as sublist retrieval, element searching, sorting, and converting between list types. Additionally, you explored string manipulation techniques including substrings, indexing, and case conversion. In the upcoming practice, you'll reinforce these concepts through hands-on exercises. Keep coding!