Skip to content
Related Articles

Related Articles

Improve Article
Kotlin mutableListOf()
  • Last Updated : 04 Jul, 2020

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() –




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 )
    }

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() –




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)
}

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 –




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"))
}

Output:



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

List first and last element –

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




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())
}

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.




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 )
}

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 –




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")))
}

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
Recommended Articles
Page :