Working of MutableList vs. mutableListOf

Abhishek Srivastava
6 min readMay 6, 2023

MutableList

Kotlin Mutable List is a collection of elements which can support modifications to the List. We can add elements to or remove elements from this Mutable List.

Now we start testing of working of it using diff. types of operation to check their working.

Note: To create a Mutable List object, It’s mandatory to enter size of the list. Otherwise it will give the compile time error.

Case 1-> If I create a MutableList class object with size 5 and assign the only value 0 to it.

val list = MutableList(5) { 0 }

(0 until list.size).forEach {
println(list[it])
}


O/P ->
0
0
0
0
0

Here you can check it has created the memory for all index and has assign the same value ( 0) to all.

Case 2-> If I create a MutableList class object with size 5 and will not assign the value to it.

val list2 = MutableList(5) {  } 

(0 until list2.size).forEach{
println(list2[it])
}

O/P->

kotlin.Unit
kotlin.Unit
kotlin.Unit
kotlin.Unit
kotlin.Unit

Here you can check now it create the memory of all 5 indexes with no value in that case it return the Unit object for all.

Case 3-> If I create a MutableList class object with size 5 and assign the two values 0 and 1 to it.

val list3 = MutableList(5) { 0;1; } 

(0 until list3.size).forEach{
println(list3[it])
}

O/P->
1
1
1
1
1

Here you can check it will override the all values from 0 to 1 for all indexes. so here we can check here only last updated values will be replaced by the all the value either we enter one value or multiple value, Only last updated value will be reflected to the Mutable list.

Case 4-> If i wanted to enter multiple value in the Mutable List.

val list5 = MutableList(5) { i -> i * 2 } 

(0 until list5.size).forEach{
println(list5[it])
}

O/P->
0
2
4
6
8

Here you can check now it assign the new values to each index till their size. So here you can insert any value using any type of operation.

Case 5-> If I create a MutableList class object with size 0 and will not assign the value to it.

val list6 = MutableList(0){}

(0 until list6.size).forEach{
println(list6[it])
}

O/P->

NOTHING

In that case it will print nothing, as no memory are assign to it.

Case 6-> If I create a MutableList class object with size 0 and assign a value 1 to it.

val list7 = MutableList(0){ 1 }

(0 until list7.size).forEach{
println(list7[it])
}

O/P->

NOTHING

Here you can check that i have given size 0 to them and also assign the value to it, But neither it given any compile time error nor run time error for that.

Case 7-> To enter value at runtime you must have assign one value with type. Otherwise it will give the compile time error.

val list8 = MutableList<Int>(5){  }

// Compile time error

Here you must have to assign a value to fix compile time error.

Now i have assign 0 on it and try to add value at run time.

 val list8 = MutableList<Int>(5){ 0 } 

(0 until list8.size).forEach{
list8[it] = it. // Assign value on each index
println(list8[it])
}

O/P->

0
1
2
3
4

Now it will assign all value at run time.

Case 8-> If I create a MutableList class object with size 5 with Int type and assign a value 0 to it. Now i will try to add element using add method .

val list8 = MutableList<Int>(5){ 0 } 

(0 until list8.size).forEach{
list8.add(it)
println(list8[it])
}

O/P->

0
0
0
0
0

So now you can check, it showing default values rather than showing updated value from for loop. Then now we have to think, If add operation has performed then where is other values. So now will change the print list .

val list8 = MutableList<Int>(5){ 0 }   // Here is not mandatory to insert the type of the list

(0 until list8.size).forEach{
list8.add(it)
println(list8)
}

O/P ->

[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 1, 2]
[0, 0, 0, 0, 0, 0, 1, 2, 3]
[0, 0, 0, 0, 0, 0, 1, 2, 3, 4]

So here you can check it has assign value after the list size, But question is how When list size is only five. In that case it should return any error.

Now it’s clear to insert value at particular index, You must have to use list[index] = value rather than list.add(value) method.

Case 9-> If We want to use value using add() method to insert item at particular index, You must have to use add() method with index and element.

val list8 = MutableList(5) { 0 }   

(0 until list8.size).forEach{
list8.add(index= it, element = it)
println(list8)
}

O/P ->

[0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0]
[0, 1, 2, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0]

Now you can check it has assign value to particular index and shifted all previous 0 values at last of the list size.

Now if i use it with list[] approach then what will happend

val list8 = MutableList(5) { 0 }   

(0 until list8.size).forEach{
list8[it] = it
println(list8)
}

O/P->

[0, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 1, 2, 0, 0]
[0, 1, 2, 3, 0]
[0, 1, 2, 3, 4]

Now you can check, In that case it has only replace the value from 0 to enter element and not shifted any value after item size.

So to add element, You must have to use list[] approach to prevent unwanted memory for list.

Case 10-> If I want to create a MutableList class object with size 5 and have not assigned any value to it. Now i want to use add() method or list[] to add element.

 val list8 = MutableList(5){  } 

(0 until list8.size).forEach{
list8.add(it) // Compile time error
OR
list8[it] = it // Compile time error
println(list8)
}

In that case it will give the compile time error for both type of add approach.

Note: Before perform operation to add element you must have to add element in list.

Case 11-> If I want to create a MutableList class object with size 0 and have assigned 0 value to it. Now i want to use list[] or add() method to add element.

val list8 = MutableList(0) { 0 }  

(0 until list8.size).forEach{
list8[it] = it
println(list8)
}

// OR

(0 until list8.size).forEach{
list8.add(index= it, element = it)
println(list8)
}

O/P->

Nothing

So now can check rather than size 0, It has not return any IndexOfBoundError.

So Ignore such type of error We can use this list to prevent app from Crashing.

What is the default size of the MutableList

I am actually confused with capacity and size. There is no implementation of using a default capacity MutableList currently in Kotlin stdlib. May be One of the reason why, As mutableListOf does not allow for default capacity is because default values in kotlin is not null.

inline fun <T> MutableList(
size: Int,
init: (index: Int) -> T
): MutableList<T>
(source)

Creates a new mutable list with the specified size, where each element is calculated by calling the specified init function.

The function init is called for each list element sequentially starting from the first one. It should return the value for a list element given its index.

val list = MutableList(3) { index -> 'A' + index }
println(list) // [A, B, C]
list.clear()
println(list) // []

I think enough now, Next chapter we will check the use of mutableListOf() and will compare both MutableList and MutableListOf().

Thanks for reading…

--

--

Abhishek Srivastava

Senior Software Engineer | Android | Java | Kotlin | Xamarin Native Android | Flutter | Go