Explain in detail about list and array with all function in kotlin

Explain in detail about list and array with all functions in kotlin

In Kotlin, lists and arrays are fundamental collections used to store ordered sequences of elements. They provide different functionalities and usage patterns. Here is a detailed explanation of both lists and arrays in Kotlin, including their functions and usage.

Lists in Kotlin

A list is an ordered collection of elements. Lists can be either mutable or immutable. The main distinction is that mutable lists can be modified (elements can be added, removed, or changed), while immutable lists cannot be modified after they are created.

Immutable Lists

Immutable lists are created using the listOf function.

   val immutableList = listOf(1, 2, 3, 4, 5)

Mutable Lists

Mutable lists are created using the mutableListOf function

   val mutableList = mutableListOf(1, 2, 3, 4, 5)

Common List Functions

1-Accessing Elements:

  • get(index: Int): Returns the element at the specified index.
  • first(): Returns the first element.
  • last(): Returns the last element

val firstElement = immutableList.first()
val lastElement = immutableList.last()
val secondElement = immutableList.get(1)

2- Searching Elements:

  • contains(element: E): Checks if the list contains the specified element.
  • indexOf(element: E): Returns the index of the first occurrence of the specified element.
  • lastIndexOf(element: E): Returns the index of the last occurrence of the specified element.
val containsThree = immutableList.contains(3)
val indexOfThree = immutableList.indexOf(3)

3- Adding Elements (for mutable lists):

  • add(element: E): Adds the specified element to the end of the list.
  • add(index: Int, element: E): Adds the specified element at the specified position in the list.
mutableList.add(6)
mutableList.add(2, 10)

4- Removing Elements (for mutable lists):

  • remove(element: E): Removes the first occurrence of the specified element.
  • removeAt(index: Int): Removes the element at the specified position in the list.
mutableList.remove(10)
mutableList.removeAt(2)

5- Modifying Elements (for mutable lists):

  • set(index: Int, element: E): Replaces the element at the specified position with the selected element.
mutableList.set(0, 100)

6- List Properties:

  • size: Returns the number of elements in the list.
  • isEmpty(): Checks if the list is empty.
  • isNotEmpty(): Checks if the list is not empty.
val listSize = immutableList.size
val isEmpty = immutableList.isEmpty()

Example:-


fun main() {
    val list = listOf("apple", "banana", "cherry")
    val mutableList = mutableListOf("apple", "banana", "cherry")

    println(list[0]) // Access element
    println(list.first()) // First element
    println(list.last()) // Last element

    mutableList.add("date")
    println(mutableList)

    mutableList.remove("banana")
    println(mutableList)

    mutableList[0] = "apricot"
    println(mutableList)
}

Out Put:-   apple

apple

cherry

[apple, banana, cherry, date]

[apple, cherry, date]

[apricot, cherry, date]

Arrays in Kotlin

Arrays are used to store multiple values of the same type. Arrays in Kotlin are fixed-size and provide more efficient memory usage than lists. They are created using the arrayOf function or specialized array functions like intArrayOf, charArrayOf, etc.


Creating Arrays

+-----------------------------------------------------------+

val intArray = arrayOf(1, 2, 3, 4, 5)                     

val stringArray = arrayOf("apple", "banana", "cherry")    

val intArraySpecific = intArrayOf(1, 2, 3, 4, 5)          

+-----------------------------------------------------------+

1- Accessing Elements:

  • get(index: Int): Returns the element at the specified index.
  • set(index: Int, value: T): Replaces the element at the specified position with the specified element.

val firstElement = intArray.get(0)
intArray.set(0, 100)

2- Array Properties:

  • size: Returns the number of elements in the array

val arraySize = intArray.size

3- Utility Functions:

  • contains(element: T): Checks if the array contains the specified element.
  • indexOf(element: T): Returns the index of the first occurrence of the specified element.
  • lastIndexOf(element: T): Returns the index of the last occurrence of the specified element.

val containsThree = intArray.contains(3)
val indexOfThree = intArray.indexOf(3)

4- Iteration:

  • forEach(action: (T) -> Unit): Performs the given action on each element.

intArray.forEach { println(it) }

Example:-

fun main() {
    val array = arrayOf("apple", "banana", "cherry")

    println(array[0]) // Access element
    array[0] = "apricot"
    println(array[0])

    val size = array.size
    println("Size of the array: $size")

    val containsBanana = array.contains("banana")
    println("Contains banana: $containsBanana")

    array.forEach { println(it) }
}

Out Put:-

apple
apricot
Size of the array: 3
Contains banana: true
apricot
banana
cherry

Summary :- 

  • Lists: Flexible size, mutable and immutable types, and more functionality for element management.
  • Arrays: Fixed size, efficient memory usage, basic functionalities for element access and modification.
Both lists and arrays are crucial for different scenarios in Kotlin programming. Lists are preferred for dynamic collections, while arrays are useful for fixed-size collections with a need for efficient memory usage.



Comments