![]() |
VOOZH | about |
In Kotlin, a List is a generic, ordered collection of elements. Lists are very common in everyday programming as they allow us to store multiple values in a single variable. Kotlin provides two types of lists - Immutable Lists (which cannot be changed and created using listOf()) and Mutable Lists (which can be modified and created using mutableListOf()).
In this article, we will focus on listOf(), which creates a read-only (immutable) list.
The listOf() function is used to create a fixed, immutable list. This means once the list is created, we cannot add, remove, or update its elements.
For Example:
val numbers = listOf(1, 2, 3, 4, 5)Here, we created an immutable list named numbers containing the integers 1 through 5.
We can access elements in the list using indexing. The index of the first element is 0 and the last element is list.size - 1.
val firstNumber = numbers[0] // returns 1
val lastNumber = numbers[numbers.size - 1] // returns 5
In this example, we use indexing to access the first and last elements of the numbers list. Note that indexing is 0-based in Kotlin, so the first element has an index of 0 and the last element has an index of numbers.size - 1.
We can loop through the list using a simple for loop:
for (number in numbers) {
println(number)
}
In this example, we use a for loop to iterate over the numbers list and print each element to the console.
Here are some advantages of using the listOf() function in Kotlin:
Here are some disadvantages of using the listOf() function in Kotlin:
A list is a generic ordered collection of elements. Kotlin has two types of lists, immutable lists (cannot be modified) and mutable lists (can be modified).
Read-only lists are created with listOf() whose elements can not be modified and mutable lists created with mutableListOf() method where we alter or modify the elements of the list.
1. List of Integers:
Output:
3
1
3
2. List of Strings:
Output:
List size: 4
Index of 'Raja': 2
Element at index 2: Raja
Ram
Shyam
Raja
Rani
Each element of a list has an index. The first element has an index of zero (0) and the last Element has index
len - 1, where 'len' is the length of the list.
Example:
Output:
1
6
First index of 1: 0
Last index of 1: 6
Last index in list: 8
We can retrieve the first and the last elements of the list without using the get() method.
Example:
Output:
1
10
It is process of accessing the elements of list one by one.
There are several ways of doing this in Kotlin.
Example:
Output:
Gopal, Asad, Shubham, Aditya, Devarsh, Nikhil, Gagan,
Gopal Asad Shubham Aditya Devarsh Nikhil Gagan
names[0] = Gopal
names[1] = Asad
names[2] = Shubham
names[3] = Aditya
names[4] = Devarsh
names[5] = Nikhil
names[6] = Gagan
Gopal Asad Shubham Aditya Devarsh Nikhil Gagan
Explanation:
for (name in names) {
print("$name, ")
}
The for loop traverses the list. In each cycle, the variable 'name' points to the next element in the list.
for (i in 0 until names.size) {
print("${names[i]} ")
}
This method uses the size of the list. The until keyword creates a range of the list indexes.
names.forEachIndexed({i, j -> println("namess[$i] = $j")})With the forEachIndexed() method, we loop over the list having index and value available in each iteration.
val it: ListIterator = names.listIterator()
while (it.hasNext()) {
val i = it.next()
print("$i ")
}
Here we use a ListIterator to iterate through the list.
The following example show how to sort a list in ascending or descending order.
Example:
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[8, 7, 6, 5, 4, 3, 2, 1, 0]
Explanation:
val asc = list.sorted()The sorted() method sorts the list in ascending order.
val desc = list.sortedDescending()The sortedDescending() method sorts the list in descending order.
This method checks whether an element exists in the list or not.
Example:
Output:
List contains 0
List does not contain 3 and -1
Explanation:
val res = list.contains(0)Checks whether the list contains 0 or not and returns true or false (her true) that is stored in res.
val result = list.containsAll(listOf(3, -1))Checks whether the list contains 3 and -1 or not.