ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Lists in Kotlin

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='25' AND `tutorial_submenu`='87' AND `tutorial_status`=1 LIMIT 1

Lists in Kotlin

📌 Lists in Kotlin

In Kotlin, a List is a collection that stores a sequence of elements. Lists are ordered and allow duplicate values. Kotlin provides two types of lists:

  1. Immutable List (List) → Read-only (cannot be modified).

  2. Mutable List (MutableList) → Can be modified (add, remove, or update elements).


✅ 1. Immutable List

  • Created using listOf().

  • Cannot be changed once initialized.

📌 Example:

kotlin

fun () { val fruits = listOf("Apple", "Banana", "Cherry") println(fruits) println("First fruit: ${fruits[0]}") println(Total Fruits: 3

  • listOf() creates a read-only list.

  • Access elements using the index (fruits[0]).

  • Use size to get the number of elements.


✅ 2. Mutable List

  • Created using mutableListOf().

  • Allows adding, removing, and modifying elements.

📌 Example:

kotlin

fun () { val numbers = mutableListOf(1, 2, 3) numbers.add(4) // Add element numbers.remove(2) // Remove element numbers[0] = 10 // Update element println(numbers)}

Output:

csharp

[10, 3, 4]

  • add() → Adds an element.

  • remove() → Removes a specific element.

  • numbers[0] = 10 → Updates the element at index 0.


✅ 3. List Functions in Kotlin

FunctionDescriptionExample
add(element)Adds an element to a mutable listlist.add(5)
remove(element)Removes an element by valuelist.remove("Banana")
removeAt(index)Removes an element by indexlist.removeAt(1)
get(index)Returns element at specified indexlist.get(0)
contains(element)Checks if list contains a valuelist.contains("Apple")
indexOf(element)Returns the index of the first matchlist.indexOf("Cherry")
lastIndexOf(element)Returns the last occurrence indexlist.lastIndexOf("Apple")
isEmpty()Checks if the list is emptylist.isEmpty()
sizeReturns the number of elementslist.size


✅ 4. Iterating Through a List

You can iterate through a list using different methods:

📌 Using For Loop:

kotlin

fun () { val colors = listOf("Red", "Green", "Blue") for (color in colors) { println(color) }}

📌 Using ForEach:

kotlin

colors.forEach { color -> println(color) }

📌 Using Indices:

kotlin

for (index in colors.indices) { println("Color at index $index is ${colors[index]}")}


✅ 5. Filtering and Sorting Lists

Kotlin provides powerful functions to manipulate lists:

📌 Filter:

kotlin

val numbers = listOf(1, 2, 3, 4, 5, 6)val evenNumbers = numbers.filter { it % 2 == 0 }println(evenNumbers) // Output: [2, 4, 6]

📌 Sort:

kotlin

val names = listOf("John", "Alice", "Bob")val sortedNames = names.sorted()println(sortedNames) // Output: [Alice, Bob, John]

📌 Map:

kotlin

val nums = listOf(1, 2, 3)val squaredNums = nums.map { it * it }println(squaredNums) // Output: [1, 4, 9]


✅ 6. Combining Lists

You can combine lists using different methods:

📌 Using Plus (+) Operator:

kotlin

val list1 = listOf(1, 2, 3)val list2 = listOf(4, 5, 6)val combinedList = list1 + list2println(combinedList) // Output: [1, 2, 3, 4, 5, 6]


✅ 7. Conclusion

  • Use listOf() for immutable lists.

  • Use mutableListOf() for mutable lists.

  • Apply functions like filter(), map(), and sorted() for manipulation.

  • Iterate using loops or forEach.

  • Combine lists using the + operator.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql