<p>In this tutorial, we will talk about Kotlin <span style="color: #008000;"><strong>List</strong></span> with the help of simple examples.</p> 
<h3><span style="color: #000080;"><strong><span id="Important_points_about_Kotlin_List_038_MutableList">List </span></strong></span></h3> 
<ul class="ul1"> 
<li class="li1">An ordered collection of elements.</li> 
<li class="li1">The ordered is maintained through indices (same as arrays). Indices start from zero(the index of the first element) and go to lastIndex which is the (list.size &#8211; 1).</li> 
<li class="li1">An element (including null) can occur more than one time in a list.</li> 
<li class="li1">Lists are of two types, <strong><span style="color: #008000;">immutable</span></strong> lists (cannot be modified) and <span style="color: #008000;"><strong>mutable</strong></span> lists (can be modified).</li> 
</ul> 
 
 
 
<h3><span style="color: #000080;"><strong>Kotlin Immutable List</strong></span></h3> 
 
 
 
<p>The <span style="color: #008000;"><strong>immutable</strong></span> list also known as <strong><span style="color: #008000;">read-only</span></strong> lists support only read-only access to the list.</p> 
<p>Immutable lists are created using <strong><span style="color: #008000;">listOf()</span></strong> and <strong><span style="color: #008000;">listOf<;T>;()</span></strong> functions.</p> 
<p> 
 
</p> 
<p> 
 
</p> 
<h4><strong><span id="Create_List_MutableList_in_Kotlin" style="color: #000080;">Create and initialize list with values</span></strong></h4> 
<p>The below example show you how to:</p> 
<ul class="ul1"> 
<li class="li1">initialize List using <span style="color: #008000;"><strong><a style="color: #008000;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/list-of.html"><span class="s2">listOf()</span></a></strong></span> (contains elements of all type), <span style="color: #008000;"><strong>listOf<;T>;()</strong></span> (contains elements of specific type) and <strong><span style="color: #008000;"><a style="color: #008000;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/list-of.html"><span class="s2">listOfNotNull()</span></a></span></strong> (contains no <span style="color: #000080;"><strong>null</strong></span> element) functions.</li> 
<li class="li1">create new List from another List with the same elements using <strong><span style="color: #008000;"><a style="color: #008000;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-list.html"><span class="s2">toList()</span></a> </span></strong>function.</li> 
</ul> 
<pre>fun main() {<br /> var list1 = <span style="color: #0000ff;"><strong>listOf</strong></span>("Arun", 10, 1.05f, 'a', null, 10)<br /> var list2 = <span style="color: #0000ff;"><strong>listOfNotNull</strong></span>("Arun", null, 10, null, 10)<br /> var nameList1 = <span style="color: #0000ff;"><strong>listOf<;String>;</strong></span>("Arun", "Neha", "Monika")<br /> val nameList2 = nameList1.<span style="color: #0000ff;"><strong>toList()</strong></span><br /><br /> println("list1: $list1")<br /> <strong><span style="color: #008000;">//list1: [Arun, 10, 1.05, a, null, 10]</span></strong><br /> println("list2: $list2")<br /> <strong><span style="color: #008000;">//list2: [Arun, 10, 10]</span></strong><br /> println("nameList1: $nameList1")<br /> <strong><span style="color: #008000;">//nameList1: [Arun, Neha, Monika]</span></strong><br /> println("nameList2: $nameList2")<br /> <span style="color: #008000;"><strong>//nameList2: [Arun, Neha, Monika]</strong></span><br />}</pre> 
<p> 
 
</p> 
<h4 class="p1"><span style="color: #000080;"><b>Access items from List </b></span></h4> 
<p class="p2">The below example show you how to:</p> 
<ul class="ul1"> 
<li class="li2">find the size of a List using <span style="color: #0000ff;"><strong>size</strong></span> property or <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html"><span class="s2">count()</span></a> </strong></span>method.</li> 
<li>get the index of last element using <span style="color: #0000ff;"><strong>lastIndex </strong></span>and the IntRange for list using <span style="color: #0000ff;"><strong>Indices.</strong></span></li> 
<li class="li2">check if a List is empty or not using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html"><span class="s2">isEmpty()</span></a></span></strong> or <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/is-not-empty.html"><span class="s2">isNotEmpty()</span></a>.</strong></span></li> 
<li class="li2">access the item at specified index in a List using operator<strong><span style="color: #0000ff;"> []</span></strong>, <strong><span style="color: #0000ff;">get()</span></strong>, <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-else.html"><span class="s2">getOrElse()</span></a></span></strong>, <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-null.html"><span class="s2">getOrNull()</span></a></span></strong></li> 
</ul> 
<pre>val num1 = listOf<;Int>;(1, 2, 3, 4, 5, 6)<br /><br /><span style="color: #0000ff;"><strong>// Properties</strong></span><br />println("List size: ${num1.<span style="color: #0000ff;"><strong>size</strong></span>}")<br /><span style="color: #008000;"><strong>//List size : 6</strong></span><br />println("Last element index: ${num1.<strong><span style="color: #0000ff;">lastIndex</span></strong>}")<br /><span style="color: #008000;"><strong>//Last element index: 5</strong></span><br />println("IntRange for list: ${num1.<strong><span style="color: #0000ff;">indices</span></strong>}")<br /><strong><span style="color: #008000;">//IntRange for list: 0..5</span></strong><br /><br /><strong><span style="color: #0000ff;">// Functions</span></strong><br />println("List size using count(): ${num1.<strong><span style="color: #0000ff;">count()</span></strong>}")<br /><span style="color: #008000;"><strong>//List size using count (): 6</strong></span><br /><br />println("Is list empty: ${num1.<strong><span style="color: #0000ff;">isEmpty()</span></strong>}")<br /><span style="color: #008000;"><strong>//Is list empty: false</strong></span><br />println("Is list not empty: ${num1.<strong><span style="color: #0000ff;">isNotEmpty()</span></strong>}")<br /><strong><span style="color: #008000;">//Is list not empty : true</span></strong><br /><br />println("Element at index 2: ${num1.<strong><span style="color: #0000ff;">get(2)</span></strong>}") <span style="color: #008000;"><strong>//Element at index 2: 3</strong></span><br />println("Element at index 3: ${<strong><span style="color: #0000ff;">num1[3]</span></strong>}") <span style="color: #008000;"><strong>//Element at index 3: 4</strong></span><br /><br />println("Element at index 3 or '-1' if index is out of bounds: ${num1.<span style="color: #0000ff;"><strong>getOrElse</strong></span>(3, { -1 })}")<br /><span style="color: #008000;"><strong>//Element at index 3 or '-1' if index is out of bounds: 4</strong></span><br />println("Element at index 6 or 'null' if index is out of bounds: ${num1.<strong><span style="color: #0000ff;">getOrNull</span></strong>(6)}")<br /><span style="color: #008000;"><strong>//Element at index 6 or 'null' if index is out of bounds: null</strong></span></pre> 
<h5><span style="color: #000080;"><strong><span id="Get_subList_of_a_List_in_Kotlin">Get subList of a List </span></strong></span></h5> 
<p>The below example show you how to:</p> 
<ul> 
<li>get subList of the List from specified index to another index (exclusive) using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/sub-list.html">subList()</a> </strong></span>method.</li> 
<li>get subList of the List by specifying the range using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/slice.html">slice()</a></span></strong> method.</li> 
</ul> 
<pre>val num1 = listOf<;Int>;(1, 2, 3, 4, 5, 6)<br /><br />println("Sublist from index 1 to 3: (exclusive) ${num1.<strong><span style="color: #0000ff;">subList</span></strong>(1, 3)}")<br /><span style="color: #008000;"><strong>//Sublist from index 1 to 3(exclusive): [2, 3]</strong></span><br /><br />println("Sublist using intRange 1..3: ${num1.<strong><span style="color: #0000ff;">slice</span></strong>(1..3)}")<br /><strong><span style="color: #008000;">//Sublist using intRange 1..3: [2, 3, 4]</span></strong></pre> 
<h5 class="p1"><span style="color: #0000ff;"><b><span style="color: #000080;">Take List elements</span></b></span></h5> 
<p>The below example show you how to:</p> 
<ul class="ul1"> 
<li class="li2">take the first or last items of a List using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/take.html"><span class="s2">take()</span></a></strong></span> or <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/take-last.html"><span class="s2">takeLast()</span></a></span></strong> function.</li> 
<li class="li2">take the first items of a List satisfying the given condition using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/take-while.html"><span class="s2">takeWhile()</span></a></span></strong> function.</li> 
<li class="li2">take the last items of a List satisfying the given condition using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/take-last-while.html"><span class="s2">takeLastWhile()</span></a></span></strong> function.</li> 
</ul> 
<pre>val num1 = listOf<;Int>;(1, 2, 3, 4, 5, 6)<br /><br />println("First three elements of a list: ${num1.<span style="color: #0000ff;"><strong>take</strong></span>(3)}")<br /><span style="color: #008000;"><strong>//First three elements of a list: [1, 2, 3]</strong></span><br />println("First items of a List satisfying the condition: ${num1.<strong><span style="color: #0000ff;">takeWhile</span></strong> { it <; 5 }}")<br /><strong><span style="color: #008000;">//First items of a List satisfying the condition: [1, 2, 3, 4]</span></strong><br />println("Last three elements of a list: ${num1.<strong><span style="color: #0000ff;">takeLast</span></strong>(3)}")<br /><strong><span style="color: #008000;">//Last three elements of a list: [4, 5, 6]</span></strong><br />println("Last items of a List satisfying the condition: ${num1.<strong><span style="color: #0000ff;">takeLastWhile</span></strong> { it >; 4 }}")<br /><span style="color: #008000;"><strong>//Last items of a List satisfying the condition: [5, 6]</strong></span></pre> 
<h5 class="p2"><span style="color: #000080;"><b>Drop List elements</b></span></h5> 
<p>The below example show you how to:</p> 
<ul class="ul1"> 
<li class="li3">get subList that contains all items by dropping first items or last items using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop.html"><span class="s2">drop()</span></a> </strong></span>or <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop-last.html"><span class="s2">dropLast()</span></a></span></strong></li> 
<li class="li3">get subList containing all items by dropping first items that satisfy a given condition using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop-while.html"><span class="s2">dropWhile()</span></a></span></strong></li> 
<li class="li3">get subList containing all items by dropping last items that satisfy a given condition using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop-last-while.html"><span class="s2">dropLastWhile()</span></a></strong></span></li> 
</ul> 
<pre>val num1 = listOf<;Int>;(1, 2, 3, 4, 5, 6)<br /><br />println("Drop first three elements of a list: ${num1.<strong><span style="color: #0000ff;">drop</span></strong>(3)}")<br /><span style="color: #008000;"><strong>//Drop first three elements of a list: [4, 5, 6]</strong></span><br />println("Drop first elements of a List satisfying the condition: ${num1.<strong><span style="color: #0000ff;">dropWhile</span></strong> { it <; 5 }}")<br /><strong><span style="color: #008000;">//Drop first elements of a List satisfying the condition: [5, 6]</span></strong><br />println("Drop last two elements of a list: ${num1.<span style="color: #0000ff;"><strong>dropLast</strong></span>(2)}")<br /><strong><span style="color: #008000;">//Drop last two elements of a list: [1, 2, 3, 4]</span></strong><br />println("Drop last elements of a List satisfying the condition: ${num1.<strong><span style="color: #0000ff;">dropLastWhile</span></strong> { it >; 4 }}")<br /><strong><span style="color: #008000;">//Drop last elements of a List satisfying the condition: [1, 2, 3, 4]</span></strong></pre> 
<h5 class="p1"><span style="color: #000080;"><b>Find elements in List</b></span></h5> 
<p class="p2">The below example show how to:</p> 
<ul class="ul1"> 
<li class="li2">find the index of the first/last occurrence of an element using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index-of.html"><span class="s2">indexOf()</span></a> / <a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last-index-of.html"><span class="s2">lastIndexOf()</span></a></span></strong></li> 
<li>find the first/last element of the list using <span style="color: #0000ff;"><strong>first()/last()</strong></span></li> 
<li class="li2">check if a List contains an element or not using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains.html"><span class="s2">contains()</span></a> </strong></span></li> 
<li class="li2">check if a List contains all given elements or not using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains-all.html"><span class="s2">containsAll()</span></a></span></strong></li> 
<li class="li2">find the index of the first/last occurrence of an element that matches a condition using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index-of-first.html"><span class="s2">indexOfFirst()</span></a> / <a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index-of-last.html"><span class="s2">indexOfLast()</span></a></span></strong></li> 
<li class="li2">find the first/last element in the list that matches a condition using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/find.html"><span class="s2">find()</span></a> / <a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/find-last.html"><span class="s2">findLast()</span></a></span></strong></li> 
</ul> 
<pre>val num2 = listOf<;Int>;(2, 3, 4, 5)<br />val num3 = listOf<;Int>;(1, 2, 3, 4, 3, 5, 1, 6)<br /><br />val strList = listOf<;String>;("book", "cook", "took", "look")<br /><br />println("Element(3) first occurrence index: ${num3.<span style="color: #0000ff;"><strong>indexOf</strong></span>(3)}")<br /><strong><span style="color: #008000;">//Element(3) first occurrence index: 2</span></strong><br /><br />println("Element(1) last occurrence index: ${num3.<strong><span style="color: #0000ff;">lastIndexOf</span></strong>(1)}")<br /><span style="color: #008000;"><strong>//Element(1) last occurrence index: 6<br /></strong></span><br />println("First element in list: ${num3.<strong><span style="color: #0000ff;">first()</span></strong>}")<br /><span style="color: #008000;"><strong>//First element in list: 1</strong></span><br />println("Last element in list: ${num3.<strong><span style="color: #0000ff;">last()</span></strong>}")<br /><strong><span style="color: #008000;">//Last element in list: 6</span></strong><br /><br />println("Does 3 exist in list: ${num3.<strong><span style="color: #0000ff;">contains(3)</span></strong>}")<br /><span style="color: #008000;"><strong>//Does 3 exist in list: true</strong></span><br />println("Does 9 exist in list: ${num3.<strong><span style="color: #0000ff;">contains(9)</span></strong>}")<br /><span style="color: #008000;"><strong>//Does 9 exist in list: false</strong></span><br /><br />println("Does all elements in num2 exist in num1: ${num3.<span style="color: #0000ff;"><strong>containsAll</strong></span>(num2)}")<br /><span style="color: #008000;"><strong>//Does all elements in num2 exist in num1: true<br /></strong></span><br />println(strList.<span style="color: #0000ff;"><strong>indexOfFirst</strong></span> { e ->; e.startsWith("coo") }) <strong><span style="color: #008000;">// 1</span></strong><br />println(strList.<strong><span style="color: #0000ff;">indexOfFirst</span></strong> { e ->; e.startsWith("oo") })<strong><span style="color: #008000;"> //-1</span></strong><br />println(strList.<strong><span style="color: #0000ff;">indexOfLast</span></strong> { e ->; e.endsWith("ook") }) <strong><span style="color: #008000;">//3</span></strong><br /><br />println(strList.<span style="color: #0000ff;"><strong>find</strong></span> { e ->; e.startsWith("to") }) <span style="color: #008000;"><strong>// took</strong></span><br />println(strList.<strong><span style="color: #0000ff;">find</span></strong> { e ->; e.startsWith("oo") }) <strong><span style="color: #008000;">// null</span></strong><br /><br />println(strList.<strong><span style="color: #0000ff;">findLast</span></strong> { e ->; e.endsWith("ook") }) <strong><span style="color: #008000;">// look</span></strong><br />println(strList.<strong><span style="color: #0000ff;">findLast</span></strong> { e ->; e.endsWith("oo") }) <span style="color: #008000;"><strong>//null</strong></span></pre> 
<h3><span style="color: #000080;"><strong>Kotlin Mutable List</strong></span></h3> 
<p><span style="color: #000080;"><strong>MutableList</strong></span> inherites <span style="color: #0000ff;"><strong>List</strong></span> and supports read/write access, you can add, update or remove items.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>
 
<p>Mutable lists are created using <strong><span style="color: #008000;">mutableListOf()</span></strong> and <span style="color: #008000;"><strong>mutableListOf()<;T>;()</strong></span> functions.</p> 
<h5><span style="color: #000080;"><strong><span id="Create_List_MutableList_in_Kotlin">Create and initialize mutableList with values</span></strong></span></h5> 
<p>The below example show you how to:</p> 
<ul class="ul1"> 
<li class="li1">initialize List using <strong><span style="color: #008000;">mutableListOf() </span></strong>(contains elements of all type), <span style="color: #008000;"><strong>mutableListOf()<;T>;()</strong></span> (contains elements of specific type).</li> 
<li class="li1">create new List from another List with the same elements using <strong><span style="color: #008000;">toMutableList() </span></strong>function.</li> 
</ul> 
<pre>fun main() {<br /> var list1 = <strong><span style="color: #0000ff;">mutableListOf</span></strong>("Arun", 10, 1.05f, 'a', null, 10)<br /> var nameList1 = <span style="color: #0000ff;"><strong>mutableListOf<;String>;</strong></span>("Arun", "Neha", "Monika")<br /> val nameList2 = nameList1.<strong><span style="color: #0000ff;">toMutableList()</span></strong><br /><br /> println("list1: $list1")<br /> <span style="color: #008000;"><strong>//list1: [Arun, 10, 1.05, a, null, 10]</strong></span><br /> println("nameList1: $nameList1")<br /> <strong><span style="color: #008000;">//nameList1: [Arun, Neha, Monika]</span></strong><br /> println("nameList2: $nameList2")<br /> <strong><span style="color: #008000;">//nameList2: [Arun, Neha, Monika]</span></strong><br />}</pre> 
<h5><span style="color: #000080;"><strong><span id="Add_items_to_List_in_Kotlin">Add items to MutableList</span></strong></span></h5> 
<p>The below example shows how to:</p> 
<ul> 
<li>add element to list using<span style="color: #0000ff;"><strong> </strong></span><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add.html"><span style="color: #0000ff;"><strong>add()</strong></span></a> method.</li> 
<li>insert element into the list at the specified index using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add.html"><span style="color: #0000ff;"><strong>add(index, element)</strong></span></a>.</li> 
<li><span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">Add all of the elements of a list to the end<span style="color: #0000ff;"><strong> </strong></span>of another list using <span style="color: #0000ff;"><strong><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add-all.html"><strong><span style="color: #0000ff;">addAll(list)</span></strong></a>.</strong></span></span></li> 
<li><span style="font-size: 16px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;"><span style="color: #0000ff;"><span style="color: #000000;">Add all of the elements of a list at specified index of another list using </span><strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add-all.html">addAll(index, list)</a></span>.</strong></span></span></li> 
</ul> 
<pre>val num1 = mutableListOf(0, 1)<br /><br />num1.<span style="color: #0000ff;"><strong>add</strong></span>(5)<br />println(num1) <span style="color: #008000;"><strong>//[0, 1, 5]</strong></span><br /><br />num1.<span style="color: #0000ff;"><strong>add</strong></span>(3, 6)<br />println(num1) <strong><span style="color: #008000;"> //[0, 1, 5, 6]</span></strong><br /><br />num1.<strong><span style="color: #0000ff;">addAll</span></strong>(listOf(7,8))<br />println(num1) <span style="color: #008000;"><strong>//[0, 1, 5, 6, 7, 8]</strong></span><br /><br />num1.<strong><span style="color: #0000ff;">addAll</span></strong>(2, listOf(4))<br />println(num1) <strong><span style="color: #008000;"> //[0, 1, 4, 5, 6, 7, 8]</span></strong></pre> 
<h5><span style="color: #000080;"><strong><span id="Kotlin_UpdateReplace_item_in_List">Update/Replace item in MutableList</span></strong></span></h5> 
<p class="p1">The examples show you how to:</p> 
<ul class="ul1"> 
<li class="li1">update/replace item in mutable List at specified index using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/set.html"><span class="s2">set()</span></a> </strong></span>method or operator<strong><span style="color: #0000ff;">[]</span></strong></li> 
<li class="li1">replace all items in mutable List with given value using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fill.html"><span class="s2">fill()</span></a> </span></strong>method.</li> 
<li class="li1">replace each item in the list with a result of a function using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/replace-all.html"><span class="s2">replaceAll()</span></a> </span></strong>method.</li> 
</ul> 
<pre>val nums = mutableListOf(0, 1, "three")<br /><br /> nums.<span style="color: #0000ff;"><strong>set</strong></span>(1, "one")<br /> println(nums) <span style="color: #008000;"><strong> //[0, one, three]</strong></span><br /><br /> <strong><span style="color: #0000ff;">nums[2]</span></strong> = "two"<br /> println(nums) <strong><span style="color: #008000;"> //[0, one, two]</span></strong><br /><br /> nums.<strong><span style="color: #0000ff;">fill</span></strong>("three")<br /> println(nums) <strong><span style="color: #008000;"> //[three, three, three]</span></strong><br /><br /> nums.<strong><span style="color: #0000ff;">replaceAll</span></strong>({ e ->; e.toString().capitalize() + ".com" })<br /> println(nums) <strong><span style="color: #008000;"> //[Three.com, Three.com, Three.com]</span></strong></pre> 
<h5><span style="color: #000080;"><strong><span id="Kotlin_remove_items_from_List">Remove items from MutableList</span></strong></span></h5> 
<p class="p1">The examples show you how to:</p> 
<ul class="ul1"> 
<li class="li1">remove the item at a given index in a List using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/remove-at.html"><span class="s2">removeAt(index)</span></a></strong></span></li> 
<li class="li1">remove the first occurrence of item from a List by its value using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/remove.html"><span class="s2">remove(value)</span></a></span></strong></li> 
<li class="li1">remove all items from the List that match a given condition or another List using <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/remove-all.html"><span class="s2">removeAll()</span></a></strong></span></li> 
<li class="li1">remove all the items using <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/clear.html"><span class="s2">clear()</span></a></span></strong></li> 
</ul> 
<pre>val num2 = mutableListOf(0, 1, 2, 3, 4, 3, 5)<br /><br />//remove items from List<br />num2.<strong><span style="color: #0000ff;">remove</span></strong>(3)<br />println(num2) <span style="color: #008000;"><strong>//[0, 1, 2, 4, 3, 5]</strong></span><br /><br />num2.<strong><span style="color: #0000ff;">removeAt</span></strong>(1)<br />println(num2) <strong><span style="color: #008000;">//[0, 2, 4, 3, 5]</span></strong><br /><br />num2.<strong><span style="color: #0000ff;">removeAll</span></strong>(listOf(1, 2, 3))<br />println(num2) <strong><span style="color: #008000;">//[0, 4, 5]</span></strong><br /><br />num2.<strong><span style="color: #0000ff;">removeAll</span></strong>({ it <; 3 })<br />println(num2) <strong><span style="color: #008000;">//[4, 5]</span></strong><br /><br />num2.<strong><span style="color: #0000ff;">clear</span></strong>()<br />println(num2) <strong><span style="color: #008000;">//[]</span></strong></pre> 
<p> 
 
</p> 
<h3 class="p1"><span style="color: #000080;"><b>Iterate over List in Kotlin</b></span></h3> 
<p class="p2">The examples show you how to iterate over a List using:</p> 
<ul class="ul1"> 
<li class="li2">simple <span style="color: #0000ff;"><strong>for</strong></span> loop</li> 
<li><span style="color: #0000ff;"><strong>for</strong></span> loop with item <strong><span style="color: #0000ff;">index</span></strong></li> 
<li><span class="s1"><a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each.html"><span class="s2"><strong><span style="color: #0000ff;">forEach()</span></strong></span></a></span><strong><span style="color: #0000ff;"> </span></strong>method.</li> 
<li><strong><span class="s1" style="color: #0000ff;"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each-indexed.html"><span class="s2">forEachIndexed()</span></a></span></strong> that gives us index and value in each iteration.</li> 
<li class="li2"><strong><span style="color: #0000ff;"><span class="s1"><a style="color: #0000ff;" href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list-iterator/index.html"><span class="s2">ListIterator</span></a></span> </span></strong>and a while loop.</li> 
</ul> 
<pre> val numbers = listOf<;Int>;(1, 2, 3)<br /><br /> print("print list using for loop: ")<br /> <strong> <span style="color: #000080;">// print list using for loop</span></strong><br /> <span style="color: #0000ff;"><strong>for</strong></span> (number in numbers) {<br /> print("$number, ")<br /> }<br /> println()<br /><br /> <span style="color: #008000;"><strong>// print list using for loop: 1, 2, 3,</strong> </span><br /><br /> <strong><span style="color: #000080;">print("for loop with item index: ")</span></strong><br /> <span style="color: #0000ff;"><strong>for</strong></span> (i in 2 until numbers.size) {<br /> print("${numbers[i]}, ")<br /> }<br /> println()<br /><br /> <span style="color: #008000;"><strong> //for loop with item index: 3, </strong></span><br /><br /> <strong><span style="color: #000080;">print("print list using forEach loop: ")</span></strong><br /> numbers.<span style="color: #0000ff;"><strong>forEach</strong></span> { i ->; print("$i, ") }<br /> println()<br /><br /> <strong><span style="color: #008000;">//print list using forEach loop: 1, 2, 3, </span></strong><br /><br /><strong><span style="color: #000080;"> //forEachIndexed() that gives us index and value in each iteration.</span></strong><br /> println("print list using forEachIndexed() : ")<br /> numbers.<strong><span style="color: #0000ff;">forEachIndexed</span></strong>({ index, i ->; println("numbers[$index] = $i") })<br /> println()<br /><br /><strong><span style="color: #008000;"> //print list using forEachIndexed() : </span></strong><br /><strong><span style="color: #008000;"> //numbers[0] = 1</span></strong><br /><strong><span style="color: #008000;"> //numbers[1] = 2</span></strong><br /><strong><span style="color: #008000;"> //numbers[2] = 3</span></strong><br /><br /> val i1: ListIterator<;Int>; = numbers.<strong><span style="color: #0000ff;">listIterator()</span></strong><br /><br /><span style="color: #000080;"><strong> println("print list using ListIterator in forward direction: ")</strong></span><br /> while (i1.<strong><span style="color: #0000ff;">hasNext()</span></strong>) {<br /> val index = i1.<strong><span style="color: #0000ff;">nextIndex()</span></strong><br /> val element = i1.<strong><span style="color: #0000ff;">next()</span></strong><br /><br /> println("nums[$index] = $element ")<br /> }<br /> println()<br /><br /><strong><span style="color: #008000;"> //print list using ListIterator in forward direction: </span></strong><br /><strong><span style="color: #008000;"> //nums[0] = 1 </span></strong><br /><strong><span style="color: #008000;"> //nums[1] = 2 </span></strong><br /><strong><span style="color: #008000;"> //nums[2] = 3</span></strong><br /><br /><strong><span style="color: #000080;"> println("print list using ListIterator in backward direction: ")</span></strong><br /> while (i1.<strong><span style="color: #0000ff;">hasPrevious()</span></strong>) {<br /> val index = i1.<strong><span style="color: #0000ff;">previousIndex()</span></strong><br /> val element = i1.<strong><span style="color: #0000ff;">previous()</span></strong><br /><br /> println("nums[$index] = $element ")<br /> }<br /> println()<br /><br /><strong><span style="color: #008000;"> //print list using ListIterator in backward direction: </span></strong><br /><strong><span style="color: #008000;"> //nums[2] = 3 </span></strong><br /><strong><span style="color: #008000;"> //nums[1] = 2 </span></strong><br /><strong><span style="color: #008000;"> //nums[0] = 1</span></strong><br /><br /> <strong><span style="color: #000080;"> // using ListIterator</span></strong><br /><strong><span style="color: #000080;"> // print list in forward direction</span></strong><br /><strong><span style="color: #000080;"> // starting at the specified index</span></strong><br /> print("print list using ListIterator starting at the specified index: ")<br /> val it: ListIterator<;Int>; = numbers.<strong><span style="color: #0000ff;">listIterator(2)</span></strong><br /> while (it.<strong><span style="color: #0000ff;">hasNext()</span></strong>) {<br /> val element = it.<strong><span style="color: #0000ff;">next()</span></strong><br /> print("$element, ")<br /> }<br /> println()<br /><br /><span style="color: #008000;"><strong> //print list using ListIterator starting at the specified index: 3,</strong></span><br />}<br /><span style="color: #008000;"><strong><br /></strong></span></pre> 
<p></p>

