Kotlin mutableListOf()

In Kotlin, mutableListOf() method is used to instantiate MutableList Interface. MutableList class is used to create mutable lists in which the elements can be added or removed. The method mutableListOf() returns an instance of MutableList Interface and takes the array of a particular type or mixed (depends on the type of MutableList instance) elements or it can be null also.

Syntax:

 fun  <T> mutableListOf( vararg elements: T): MutableList <T>

Parameters:
It takes array of particular type or mixed type or null parameters. Null parameters is used when there is a need to create empty instance of MutableList.



Returns:

It returns the instance of MutableList Interface.

Kotlin program to demonstrate mutableListOf() –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>)
    {
        //declaring a mutable list of integers
        val mutableListA = mutableListOf<Int>( 1 , 2 , 3 , 4 , 3);
        println(mutableListA)
  
        //declaring a mutable list of strings
        val mutableListB = mutableListOf<String>("Geeks","for" , "geeks");
        println(mutableListB)
  
        //declaring an empty mutable list of integers
        val mutableListC = mutableSetOf<Int>()
        println("Empty list "+mutableListC )
    }

chevron_right


Output:

[1, 2, 3, 4, 3]
[Geeks, for, geeks]
Empty list []

Adding and removing elements in a List –

We can add elements in a mutable list using the add() function, and remove an elements using remove () function.

Kotlin program to demonstrate mutableListOf() –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>) {
    var mutablelist=mutableListOf("Geeks", "For");
    //adding string elements
    mutablelist.add("Geeks")
    for(i in mutablelist)
        println(i)
    println("... after removing \"For\" ...")
    //removing "For"
    mutablelist.remove("For")
    for(i in mutablelist)
        println(i)
}

chevron_right


Output:

Geeks
For
Geeks
... after removing "For" ...
Geeks
Geeks

Set Indexing –

Using index functions indexOf() , lastIndexOf() we can get the index of the specified element. And we can also find the elements at some specific index using elementAt() function.

Kotlin program of using index –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>)
{
    val captains = mutableListOf("Kohli","Smith","Root","Rohit","Dhawan")
  
    println("The element at index 2 is: "+captains.elementAt(2))
  
    println("The index of element is: "+captains.indexOf("Smith"))
  
    println("The last index of element is: "+captains.lastIndexOf("Rohit"))
}

chevron_right


Output:


The element at index 2 is: Root
The index of element is: 1
The last index of element is: 4

List first and last element –

We can get the first and element of a list using first() and last() functions.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>){
    val captains = mutableListOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Dhawan","Rohit")
  
    println("The first element of the list is: "+captains.first())
  
    println("The last element of the list is: "+captains.last())
}

chevron_right


Output:

The first element of the list is: 1
The last element of the list is: Rohit

Traversal in a mutableList –

We can run a for loop with an iterator which traverse all the items in the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>)
{
    //declaring a mutable list of integers
    val seta = mutableListOf( 1 , 2 , 3 , 4 );
  
    //traversal of seta using an iterator 'item'
    for(item in seta)
        println( item )
}

chevron_right


Output:

1
2
3
4

contains() and containsAll() functions –

Both the methods are used to check whether an element is present in the list or not?

Kotlin program of using contains() and containsAll() function –

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args: Array<String>){
    val captains = mutableListOf(1,2,3,4,"Kohli","Smith",
        "Root","Malinga","Rohit","Dhawan")
  
  
    var name = "Dhawan"
    println("The list contains the element $name or not?" +
            "   "+captains.contains(name))
  
    var num = 5
    println("The list contains the element $num or not?" +
            "   "+captains.contains(num))
  
    println("The list contains the given elements or not?" +
            "   "+captains.containsAll(setOf(1,3,"Root")))
}

chevron_right


Output:

The list contains the element Dhawan or not?   true
The list contains the element 5 or not?   false
The list contains the given elements or not?   true


My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.