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
- 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.
- 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.
- remove(element: E): Removes the first occurrence of the specified element.
- removeAt(index: Int): Removes the element at the specified position in the list.
- set(index: Int, element: E): Replaces the element at the specified position with the selected element.
- size: Returns the number of elements in the list.
- isEmpty(): Checks if the list is empty.
- isNotEmpty(): Checks if the list is not empty.
Example:-
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.
4- Iteration:
- forEach(action: (T) -> Unit): Performs the given action on each element.
Example:-
Out Put:-
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.
Comments
Post a Comment