Site icon C1CTech

Kotlin List

&NewLine;<p>In this tutorial&comma; we will talk about Kotlin <span style&equals;"color&colon; &num;008000&semi;"><strong>List<&sol;strong><&sol;span> with the help of simple examples&period;<&sol;p> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"Important&lowbar;points&lowbar;about&lowbar;Kotlin&lowbar;List&lowbar;038&lowbar;MutableList">List <&sol;span><&sol;strong><&sol;span><&sol;h3> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li1">An ordered collection of elements&period;<&sol;li> &NewLine;<li class&equals;"li1">The ordered is maintained through indices &lpar;same as arrays&rpar;&period; Indices start from zero&lpar;the index of the first element&rpar; and go to lastIndex which is the &lpar;list&period;size &&num;8211&semi; 1&rpar;&period;<&sol;li> &NewLine;<li class&equals;"li1">An element &lpar;including null&rpar; can occur more than one time in a list&period;<&sol;li> &NewLine;<li class&equals;"li1">Lists are of two types&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">immutable<&sol;span><&sol;strong> lists &lpar;cannot be modified&rpar; and <span style&equals;"color&colon; &num;008000&semi;"><strong>mutable<&sol;strong><&sol;span> lists &lpar;can be modified&rpar;&period;<&sol;li> &NewLine;<&sol;ul> &NewLine; &NewLine; &NewLine; &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Kotlin Immutable List<&sol;strong><&sol;span><&sol;h3> &NewLine; &NewLine; &NewLine; &NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>immutable<&sol;strong><&sol;span> list also known as <strong><span style&equals;"color&colon; &num;008000&semi;">read-only<&sol;span><&sol;strong> lists support only read-only access to the list&period;<&sol;p> &NewLine;<p>Immutable lists are created using <strong><span style&equals;"color&colon; &num;008000&semi;">listOf&lpar;&rpar;<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">listOf&lt&semi;T&gt&semi;&lpar;&rpar;<&sol;span><&sol;strong> functions&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h4><strong><span id&equals;"Create&lowbar;List&lowbar;MutableList&lowbar;in&lowbar;Kotlin" style&equals;"color&colon; &num;000080&semi;">Create and initialize list with values<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>The below example show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li1">initialize List using <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;list-of&period;html"><span class&equals;"s2">listOf&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span> &lpar;contains elements of all type&rpar;&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>listOf&lt&semi;T&gt&semi;&lpar;&rpar;<&sol;strong><&sol;span> &lpar;contains elements of specific type&rpar; and <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;list-of&period;html"><span class&equals;"s2">listOfNotNull&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> &lpar;contains no <span style&equals;"color&colon; &num;000080&semi;"><strong>null<&sol;strong><&sol;span> element&rpar; functions&period;<&sol;li> &NewLine;<li class&equals;"li1">create new List from another List with the same elements using <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;to-list&period;html"><span class&equals;"s2">toList&lpar;&rpar;<&sol;span><&sol;a> <&sol;span><&sol;strong>function&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>fun main&lpar;&rpar; &lbrace;<br &sol;> var list1 &equals; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>listOf<&sol;strong><&sol;span>&lpar;"Arun"&comma; 10&comma; 1&period;05f&comma; 'a'&comma; null&comma; 10&rpar;<br &sol;> var list2 &equals; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>listOfNotNull<&sol;strong><&sol;span>&lpar;"Arun"&comma; null&comma; 10&comma; null&comma; 10&rpar;<br &sol;> var nameList1 &equals; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>listOf&lt&semi;String&gt&semi;<&sol;strong><&sol;span>&lpar;"Arun"&comma; "Neha"&comma; "Monika"&rpar;<br &sol;> val nameList2 &equals; nameList1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>toList&lpar;&rpar;<&sol;strong><&sol;span><br &sol;><br &sol;> println&lpar;"list1&colon; &dollar;list1"&rpar;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;list1&colon; &lbrack;Arun&comma; 10&comma; 1&period;05&comma; a&comma; null&comma; 10&rsqb;<&sol;span><&sol;strong><br &sol;> println&lpar;"list2&colon; &dollar;list2"&rpar;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;list2&colon; &lbrack;Arun&comma; 10&comma; 10&rsqb;<&sol;span><&sol;strong><br &sol;> println&lpar;"nameList1&colon; &dollar;nameList1"&rpar;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;nameList1&colon; &lbrack;Arun&comma; Neha&comma; Monika&rsqb;<&sol;span><&sol;strong><br &sol;> println&lpar;"nameList2&colon; &dollar;nameList2"&rpar;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;nameList2&colon; &lbrack;Arun&comma; Neha&comma; Monika&rsqb;<&sol;strong><&sol;span><br &sol;>&rcub;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Access items from List <&sol;b><&sol;span><&sol;h4> &NewLine;<p class&equals;"p2">The below example show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li2">find the size of a List using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>size<&sol;strong><&sol;span> property or <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;count&period;html"><span class&equals;"s2">count&lpar;&rpar;<&sol;span><&sol;a> <&sol;strong><&sol;span>method&period;<&sol;li> &NewLine;<li>get the index of last element using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>lastIndex <&sol;strong><&sol;span>and the IntRange for list using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Indices&period;<&sol;strong><&sol;span><&sol;li> &NewLine;<li class&equals;"li2">check if a List is empty or not using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-collection&sol;is-empty&period;html"><span class&equals;"s2">isEmpty&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> or <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;is-not-empty&period;html"><span class&equals;"s2">isNotEmpty&lpar;&rpar;<&sol;span><&sol;a>&period;<&sol;strong><&sol;span><&sol;li> &NewLine;<li class&equals;"li2">access the item at specified index in a List using operator<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lbrack;&rsqb;<&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;">get&lpar;&rpar;<&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;get-or-else&period;html"><span class&equals;"s2">getOrElse&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;get-or-null&period;html"><span class&equals;"s2">getOrNull&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num1 &equals; listOf&lt&semi;Int&gt&semi;&lpar;1&comma; 2&comma; 3&comma; 4&comma; 5&comma; 6&rpar;<br &sol;><br &sol;><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol; Properties<&sol;strong><&sol;span><br &sol;>println&lpar;"List size&colon; &dollar;&lbrace;num1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>size<&sol;strong><&sol;span>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;List size &colon; 6<&sol;strong><&sol;span><br &sol;>println&lpar;"Last element index&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">lastIndex<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Last element index&colon; 5<&sol;strong><&sol;span><br &sol;>println&lpar;"IntRange for list&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">indices<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;IntRange for list&colon; 0&period;&period;5<&sol;span><&sol;strong><br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; Functions<&sol;span><&sol;strong><br &sol;>println&lpar;"List size using count&lpar;&rpar;&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">count&lpar;&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;List size using count &lpar;&rpar;&colon; 6<&sol;strong><&sol;span><br &sol;><br &sol;>println&lpar;"Is list empty&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">isEmpty&lpar;&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Is list empty&colon; false<&sol;strong><&sol;span><br &sol;>println&lpar;"Is list not empty&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">isNotEmpty&lpar;&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Is list not empty &colon; true<&sol;span><&sol;strong><br &sol;><br &sol;>println&lpar;"Element at index 2&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">get&lpar;2&rpar;<&sol;span><&sol;strong>&rcub;"&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Element at index 2&colon; 3<&sol;strong><&sol;span><br &sol;>println&lpar;"Element at index 3&colon; &dollar;&lbrace;<strong><span style&equals;"color&colon; &num;0000ff&semi;">num1&lbrack;3&rsqb;<&sol;span><&sol;strong>&rcub;"&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Element at index 3&colon; 4<&sol;strong><&sol;span><br &sol;><br &sol;>println&lpar;"Element at index 3 or '-1' if index is out of bounds&colon; &dollar;&lbrace;num1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>getOrElse<&sol;strong><&sol;span>&lpar;3&comma; &lbrace; -1 &rcub;&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Element at index 3 or '-1' if index is out of bounds&colon; 4<&sol;strong><&sol;span><br &sol;>println&lpar;"Element at index 6 or 'null' if index is out of bounds&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">getOrNull<&sol;span><&sol;strong>&lpar;6&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Element at index 6 or 'null' if index is out of bounds&colon; null<&sol;strong><&sol;span><&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"Get&lowbar;subList&lowbar;of&lowbar;a&lowbar;List&lowbar;in&lowbar;Kotlin">Get subList of a List <&sol;span><&sol;strong><&sol;span><&sol;h5> &NewLine;<p>The below example show you how to&colon;<&sol;p> &NewLine;<ul> &NewLine;<li>get subList of the List from specified index to another index &lpar;exclusive&rpar; using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;sub-list&period;html">subList&lpar;&rpar;<&sol;a> <&sol;strong><&sol;span>method&period;<&sol;li> &NewLine;<li>get subList of the List by specifying the range using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;slice&period;html">slice&lpar;&rpar;<&sol;a><&sol;span><&sol;strong> method&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num1 &equals; listOf&lt&semi;Int&gt&semi;&lpar;1&comma; 2&comma; 3&comma; 4&comma; 5&comma; 6&rpar;<br &sol;><br &sol;>println&lpar;"Sublist from index 1 to 3&colon; &lpar;exclusive&rpar; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">subList<&sol;span><&sol;strong>&lpar;1&comma; 3&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Sublist from index 1 to 3&lpar;exclusive&rpar;&colon; &lbrack;2&comma; 3&rsqb;<&sol;strong><&sol;span><br &sol;><br &sol;>println&lpar;"Sublist using intRange 1&period;&period;3&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">slice<&sol;span><&sol;strong>&lpar;1&period;&period;3&rpar;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Sublist using intRange 1&period;&period;3&colon; &lbrack;2&comma; 3&comma; 4&rsqb;<&sol;span><&sol;strong><&sol;pre> &NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b><span style&equals;"color&colon; &num;000080&semi;">Take List elements<&sol;span><&sol;b><&sol;span><&sol;h5> &NewLine;<p>The below example show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li2">take the first or last items of a List using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;take&period;html"><span class&equals;"s2">take&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span> or <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;take-last&period;html"><span class&equals;"s2">takeLast&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> function&period;<&sol;li> &NewLine;<li class&equals;"li2">take the first items of a List satisfying the given condition using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;take-while&period;html"><span class&equals;"s2">takeWhile&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> function&period;<&sol;li> &NewLine;<li class&equals;"li2">take the last items of a List satisfying the given condition using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;take-last-while&period;html"><span class&equals;"s2">takeLastWhile&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> function&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num1 &equals; listOf&lt&semi;Int&gt&semi;&lpar;1&comma; 2&comma; 3&comma; 4&comma; 5&comma; 6&rpar;<br &sol;><br &sol;>println&lpar;"First three elements of a list&colon; &dollar;&lbrace;num1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>take<&sol;strong><&sol;span>&lpar;3&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;First three elements of a list&colon; &lbrack;1&comma; 2&comma; 3&rsqb;<&sol;strong><&sol;span><br &sol;>println&lpar;"First items of a List satisfying the condition&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">takeWhile<&sol;span><&sol;strong> &lbrace; it &lt&semi; 5 &rcub;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;First items of a List satisfying the condition&colon; &lbrack;1&comma; 2&comma; 3&comma; 4&rsqb;<&sol;span><&sol;strong><br &sol;>println&lpar;"Last three elements of a list&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">takeLast<&sol;span><&sol;strong>&lpar;3&rpar;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Last three elements of a list&colon; &lbrack;4&comma; 5&comma; 6&rsqb;<&sol;span><&sol;strong><br &sol;>println&lpar;"Last items of a List satisfying the condition&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">takeLastWhile<&sol;span><&sol;strong> &lbrace; it &gt&semi; 4 &rcub;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Last items of a List satisfying the condition&colon; &lbrack;5&comma; 6&rsqb;<&sol;strong><&sol;span><&sol;pre> &NewLine;<h5 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Drop List elements<&sol;b><&sol;span><&sol;h5> &NewLine;<p>The below example show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li3">get subList that contains all items by dropping first items or last items using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;drop&period;html"><span class&equals;"s2">drop&lpar;&rpar;<&sol;span><&sol;a> <&sol;strong><&sol;span>or <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;drop-last&period;html"><span class&equals;"s2">dropLast&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<li class&equals;"li3">get subList containing all items by dropping first items that satisfy a given condition using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;drop-while&period;html"><span class&equals;"s2">dropWhile&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<li class&equals;"li3">get subList containing all items by dropping last items that satisfy a given condition using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;drop-last-while&period;html"><span class&equals;"s2">dropLastWhile&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span><&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num1 &equals; listOf&lt&semi;Int&gt&semi;&lpar;1&comma; 2&comma; 3&comma; 4&comma; 5&comma; 6&rpar;<br &sol;><br &sol;>println&lpar;"Drop first three elements of a list&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">drop<&sol;span><&sol;strong>&lpar;3&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Drop first three elements of a list&colon; &lbrack;4&comma; 5&comma; 6&rsqb;<&sol;strong><&sol;span><br &sol;>println&lpar;"Drop first elements of a List satisfying the condition&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">dropWhile<&sol;span><&sol;strong> &lbrace; it &lt&semi; 5 &rcub;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Drop first elements of a List satisfying the condition&colon; &lbrack;5&comma; 6&rsqb;<&sol;span><&sol;strong><br &sol;>println&lpar;"Drop last two elements of a list&colon; &dollar;&lbrace;num1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>dropLast<&sol;strong><&sol;span>&lpar;2&rpar;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Drop last two elements of a list&colon; &lbrack;1&comma; 2&comma; 3&comma; 4&rsqb;<&sol;span><&sol;strong><br &sol;>println&lpar;"Drop last elements of a List satisfying the condition&colon; &dollar;&lbrace;num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">dropLastWhile<&sol;span><&sol;strong> &lbrace; it &gt&semi; 4 &rcub;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Drop last elements of a List satisfying the condition&colon; &lbrack;1&comma; 2&comma; 3&comma; 4&rsqb;<&sol;span><&sol;strong><&sol;pre> &NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Find elements in List<&sol;b><&sol;span><&sol;h5> &NewLine;<p class&equals;"p2">The below example show how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li2">find the index of the first&sol;last occurrence of an element using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;index-of&period;html"><span class&equals;"s2">indexOf&lpar;&rpar;<&sol;span><&sol;a> &sol; <a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;last-index-of&period;html"><span class&equals;"s2">lastIndexOf&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<li>find the first&sol;last element of the list using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>first&lpar;&rpar;&sol;last&lpar;&rpar;<&sol;strong><&sol;span><&sol;li> &NewLine;<li class&equals;"li2">check if a List contains an element or not using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;contains&period;html"><span class&equals;"s2">contains&lpar;&rpar;<&sol;span><&sol;a> <&sol;strong><&sol;span><&sol;li> &NewLine;<li class&equals;"li2">check if a List contains all given elements or not using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;contains-all&period;html"><span class&equals;"s2">containsAll&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<li class&equals;"li2">find the index of the first&sol;last occurrence of an element that matches a condition using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;index-of-first&period;html"><span class&equals;"s2">indexOfFirst&lpar;&rpar;<&sol;span><&sol;a> &sol; <a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;index-of-last&period;html"><span class&equals;"s2">indexOfLast&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<li class&equals;"li2">find the first&sol;last element in the list that matches a condition using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;find&period;html"><span class&equals;"s2">find&lpar;&rpar;<&sol;span><&sol;a> &sol; <a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;find-last&period;html"><span class&equals;"s2">findLast&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num2 &equals; listOf&lt&semi;Int&gt&semi;&lpar;2&comma; 3&comma; 4&comma; 5&rpar;<br &sol;>val num3 &equals; listOf&lt&semi;Int&gt&semi;&lpar;1&comma; 2&comma; 3&comma; 4&comma; 3&comma; 5&comma; 1&comma; 6&rpar;<br &sol;><br &sol;>val strList &equals; listOf&lt&semi;String&gt&semi;&lpar;"book"&comma; "cook"&comma; "took"&comma; "look"&rpar;<br &sol;><br &sol;>println&lpar;"Element&lpar;3&rpar; first occurrence index&colon; &dollar;&lbrace;num3&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>indexOf<&sol;strong><&sol;span>&lpar;3&rpar;&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Element&lpar;3&rpar; first occurrence index&colon; 2<&sol;span><&sol;strong><br &sol;><br &sol;>println&lpar;"Element&lpar;1&rpar; last occurrence index&colon; &dollar;&lbrace;num3&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">lastIndexOf<&sol;span><&sol;strong>&lpar;1&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Element&lpar;1&rpar; last occurrence index&colon; 6<br &sol;><&sol;strong><&sol;span><br &sol;>println&lpar;"First element in list&colon; &dollar;&lbrace;num3&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">first&lpar;&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;First element in list&colon; 1<&sol;strong><&sol;span><br &sol;>println&lpar;"Last element in list&colon; &dollar;&lbrace;num3&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">last&lpar;&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Last element in list&colon; 6<&sol;span><&sol;strong><br &sol;><br &sol;>println&lpar;"Does 3 exist in list&colon; &dollar;&lbrace;num3&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">contains&lpar;3&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Does 3 exist in list&colon; true<&sol;strong><&sol;span><br &sol;>println&lpar;"Does 9 exist in list&colon; &dollar;&lbrace;num3&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">contains&lpar;9&rpar;<&sol;span><&sol;strong>&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Does 9 exist in list&colon; false<&sol;strong><&sol;span><br &sol;><br &sol;>println&lpar;"Does all elements in num2 exist in num1&colon; &dollar;&lbrace;num3&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>containsAll<&sol;strong><&sol;span>&lpar;num2&rpar;&rcub;"&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Does all elements in num2 exist in num1&colon; true<br &sol;><&sol;strong><&sol;span><br &sol;>println&lpar;strList&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>indexOfFirst<&sol;strong><&sol;span> &lbrace; e -&gt&semi; e&period;startsWith&lpar;"coo"&rpar; &rcub;&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; 1<&sol;span><&sol;strong><br &sol;>println&lpar;strList&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">indexOfFirst<&sol;span><&sol;strong> &lbrace; e -&gt&semi; e&period;startsWith&lpar;"oo"&rpar; &rcub;&rpar;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;-1<&sol;span><&sol;strong><br &sol;>println&lpar;strList&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">indexOfLast<&sol;span><&sol;strong> &lbrace; e -&gt&semi; e&period;endsWith&lpar;"ook"&rpar; &rcub;&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;3<&sol;span><&sol;strong><br &sol;><br &sol;>println&lpar;strList&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>find<&sol;strong><&sol;span> &lbrace; e -&gt&semi; e&period;startsWith&lpar;"to"&rpar; &rcub;&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; took<&sol;strong><&sol;span><br &sol;>println&lpar;strList&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">find<&sol;span><&sol;strong> &lbrace; e -&gt&semi; e&period;startsWith&lpar;"oo"&rpar; &rcub;&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; null<&sol;span><&sol;strong><br &sol;><br &sol;>println&lpar;strList&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">findLast<&sol;span><&sol;strong> &lbrace; e -&gt&semi; e&period;endsWith&lpar;"ook"&rpar; &rcub;&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; look<&sol;span><&sol;strong><br &sol;>println&lpar;strList&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">findLast<&sol;span><&sol;strong> &lbrace; e -&gt&semi; e&period;endsWith&lpar;"oo"&rpar; &rcub;&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;null<&sol;strong><&sol;span><&sol;pre> &NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Kotlin Mutable List<&sol;strong><&sol;span><&sol;h3> &NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>MutableList<&sol;strong><&sol;span> inherites <span style&equals;"color&colon; &num;0000ff&semi;"><strong>List<&sol;strong><&sol;span> and supports read&sol;write access&comma; you can add&comma; update or remove items&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<p>Mutable lists are created using <strong><span style&equals;"color&colon; &num;008000&semi;">mutableListOf&lpar;&rpar;<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><strong>mutableListOf&lpar;&rpar;&lt&semi;T&gt&semi;&lpar;&rpar;<&sol;strong><&sol;span> functions&period;<&sol;p> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"Create&lowbar;List&lowbar;MutableList&lowbar;in&lowbar;Kotlin">Create and initialize mutableList with values<&sol;span><&sol;strong><&sol;span><&sol;h5> &NewLine;<p>The below example show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li1">initialize List using <strong><span style&equals;"color&colon; &num;008000&semi;">mutableListOf&lpar;&rpar; <&sol;span><&sol;strong>&lpar;contains elements of all type&rpar;&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>mutableListOf&lpar;&rpar;&lt&semi;T&gt&semi;&lpar;&rpar;<&sol;strong><&sol;span> &lpar;contains elements of specific type&rpar;&period;<&sol;li> &NewLine;<li class&equals;"li1">create new List from another List with the same elements using <strong><span style&equals;"color&colon; &num;008000&semi;">toMutableList&lpar;&rpar; <&sol;span><&sol;strong>function&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>fun main&lpar;&rpar; &lbrace;<br &sol;> var list1 &equals; <strong><span style&equals;"color&colon; &num;0000ff&semi;">mutableListOf<&sol;span><&sol;strong>&lpar;"Arun"&comma; 10&comma; 1&period;05f&comma; 'a'&comma; null&comma; 10&rpar;<br &sol;> var nameList1 &equals; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>mutableListOf&lt&semi;String&gt&semi;<&sol;strong><&sol;span>&lpar;"Arun"&comma; "Neha"&comma; "Monika"&rpar;<br &sol;> val nameList2 &equals; nameList1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">toMutableList&lpar;&rpar;<&sol;span><&sol;strong><br &sol;><br &sol;> println&lpar;"list1&colon; &dollar;list1"&rpar;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;list1&colon; &lbrack;Arun&comma; 10&comma; 1&period;05&comma; a&comma; null&comma; 10&rsqb;<&sol;strong><&sol;span><br &sol;> println&lpar;"nameList1&colon; &dollar;nameList1"&rpar;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;nameList1&colon; &lbrack;Arun&comma; Neha&comma; Monika&rsqb;<&sol;span><&sol;strong><br &sol;> println&lpar;"nameList2&colon; &dollar;nameList2"&rpar;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;nameList2&colon; &lbrack;Arun&comma; Neha&comma; Monika&rsqb;<&sol;span><&sol;strong><br &sol;>&rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"Add&lowbar;items&lowbar;to&lowbar;List&lowbar;in&lowbar;Kotlin">Add items to MutableList<&sol;span><&sol;strong><&sol;span><&sol;h5> &NewLine;<p>The below example shows how to&colon;<&sol;p> &NewLine;<ul> &NewLine;<li>add element to list using<span style&equals;"color&colon; &num;0000ff&semi;"><strong> <&sol;strong><&sol;span><a href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;add&period;html"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>add&lpar;&rpar;<&sol;strong><&sol;span><&sol;a> method&period;<&sol;li> &NewLine;<li>insert element into the list at the specified index using <a href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;add&period;html"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>add&lpar;index&comma; element&rpar;<&sol;strong><&sol;span><&sol;a>&period;<&sol;li> &NewLine;<li><span style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi;">Add all of the elements of a list to the end<span style&equals;"color&colon; &num;0000ff&semi;"><strong> <&sol;strong><&sol;span>of another list using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;add-all&period;html"><strong><span style&equals;"color&colon; &num;0000ff&semi;">addAll&lpar;list&rpar;<&sol;span><&sol;strong><&sol;a>&period;<&sol;strong><&sol;span><&sol;span><&sol;li> &NewLine;<li><span style&equals;"font-size&colon; 16px&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000000&semi;">Add all of the elements of a list at specified index of another list using <&sol;span><strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;add-all&period;html">addAll&lpar;index&comma; list&rpar;<&sol;a><&sol;span>&period;<&sol;strong><&sol;span><&sol;span><&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num1 &equals; mutableListOf&lpar;0&comma; 1&rpar;<br &sol;><br &sol;>num1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>add<&sol;strong><&sol;span>&lpar;5&rpar;<br &sol;>println&lpar;num1&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;&lbrack;0&comma; 1&comma; 5&rsqb;<&sol;strong><&sol;span><br &sol;><br &sol;>num1&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>add<&sol;strong><&sol;span>&lpar;3&comma; 6&rpar;<br &sol;>println&lpar;num1&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;&lbrack;0&comma; 1&comma; 5&comma; 6&rsqb;<&sol;span><&sol;strong><br &sol;><br &sol;>num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">addAll<&sol;span><&sol;strong>&lpar;listOf&lpar;7&comma;8&rpar;&rpar;<br &sol;>println&lpar;num1&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;&lbrack;0&comma; 1&comma; 5&comma; 6&comma; 7&comma; 8&rsqb;<&sol;strong><&sol;span><br &sol;><br &sol;>num1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">addAll<&sol;span><&sol;strong>&lpar;2&comma; listOf&lpar;4&rpar;&rpar;<br &sol;>println&lpar;num1&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;&lbrack;0&comma; 1&comma; 4&comma; 5&comma; 6&comma; 7&comma; 8&rsqb;<&sol;span><&sol;strong><&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"Kotlin&lowbar;UpdateReplace&lowbar;item&lowbar;in&lowbar;List">Update&sol;Replace item in MutableList<&sol;span><&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">The examples show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li1">update&sol;replace item in mutable List at specified index using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;set&period;html"><span class&equals;"s2">set&lpar;&rpar;<&sol;span><&sol;a> <&sol;strong><&sol;span>method or operator<strong><span style&equals;"color&colon; &num;0000ff&semi;">&lbrack;&rsqb;<&sol;span><&sol;strong><&sol;li> &NewLine;<li class&equals;"li1">replace all items in mutable List with given value using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;fill&period;html"><span class&equals;"s2">fill&lpar;&rpar;<&sol;span><&sol;a> <&sol;span><&sol;strong>method&period;<&sol;li> &NewLine;<li class&equals;"li1">replace each item in the list with a result of a function using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;replace-all&period;html"><span class&equals;"s2">replaceAll&lpar;&rpar;<&sol;span><&sol;a> <&sol;span><&sol;strong>method&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val nums &equals; mutableListOf&lpar;0&comma; 1&comma; "three"&rpar;<br &sol;><br &sol;> nums&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>set<&sol;strong><&sol;span>&lpar;1&comma; "one"&rpar;<br &sol;> println&lpar;nums&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;&lbrack;0&comma; one&comma; three&rsqb;<&sol;strong><&sol;span><br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;0000ff&semi;">nums&lbrack;2&rsqb;<&sol;span><&sol;strong> &equals; "two"<br &sol;> println&lpar;nums&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;&lbrack;0&comma; one&comma; two&rsqb;<&sol;span><&sol;strong><br &sol;><br &sol;> nums&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">fill<&sol;span><&sol;strong>&lpar;"three"&rpar;<br &sol;> println&lpar;nums&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;&lbrack;three&comma; three&comma; three&rsqb;<&sol;span><&sol;strong><br &sol;><br &sol;> nums&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">replaceAll<&sol;span><&sol;strong>&lpar;&lbrace; e -&gt&semi; e&period;toString&lpar;&rpar;&period;capitalize&lpar;&rpar; &plus; "&period;com" &rcub;&rpar;<br &sol;> println&lpar;nums&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;&lbrack;Three&period;com&comma; Three&period;com&comma; Three&period;com&rsqb;<&sol;span><&sol;strong><&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong><span id&equals;"Kotlin&lowbar;remove&lowbar;items&lowbar;from&lowbar;List">Remove items from MutableList<&sol;span><&sol;strong><&sol;span><&sol;h5> &NewLine;<p class&equals;"p1">The examples show you how to&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li1">remove the item at a given index in a List using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;remove-at&period;html"><span class&equals;"s2">removeAt&lpar;index&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span><&sol;li> &NewLine;<li class&equals;"li1">remove the first occurrence of item from a List by its value using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;remove&period;html"><span class&equals;"s2">remove&lpar;value&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<li class&equals;"li1">remove all items from the List that match a given condition or another List using <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;remove-all&period;html"><span class&equals;"s2">removeAll&lpar;&rpar;<&sol;span><&sol;a><&sol;strong><&sol;span><&sol;li> &NewLine;<li class&equals;"li1">remove all the items using <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-mutable-list&sol;clear&period;html"><span class&equals;"s2">clear&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong><&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>val num2 &equals; mutableListOf&lpar;0&comma; 1&comma; 2&comma; 3&comma; 4&comma; 3&comma; 5&rpar;<br &sol;><br &sol;>&sol;&sol;remove items from List<br &sol;>num2&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">remove<&sol;span><&sol;strong>&lpar;3&rpar;<br &sol;>println&lpar;num2&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;&lbrack;0&comma; 1&comma; 2&comma; 4&comma; 3&comma; 5&rsqb;<&sol;strong><&sol;span><br &sol;><br &sol;>num2&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">removeAt<&sol;span><&sol;strong>&lpar;1&rpar;<br &sol;>println&lpar;num2&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;&lbrack;0&comma; 2&comma; 4&comma; 3&comma; 5&rsqb;<&sol;span><&sol;strong><br &sol;><br &sol;>num2&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">removeAll<&sol;span><&sol;strong>&lpar;listOf&lpar;1&comma; 2&comma; 3&rpar;&rpar;<br &sol;>println&lpar;num2&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;&lbrack;0&comma; 4&comma; 5&rsqb;<&sol;span><&sol;strong><br &sol;><br &sol;>num2&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">removeAll<&sol;span><&sol;strong>&lpar;&lbrace; it &lt&semi; 3 &rcub;&rpar;<br &sol;>println&lpar;num2&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;&lbrack;4&comma; 5&rsqb;<&sol;span><&sol;strong><br &sol;><br &sol;>num2&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">clear<&sol;span><&sol;strong>&lpar;&rpar;<br &sol;>println&lpar;num2&rpar; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;&lbrack;&rsqb;<&sol;span><&sol;strong><&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><b>Iterate over List in Kotlin<&sol;b><&sol;span><&sol;h3> &NewLine;<p class&equals;"p2">The examples show you how to iterate over a List using&colon;<&sol;p> &NewLine;<ul class&equals;"ul1"> &NewLine;<li class&equals;"li2">simple <span style&equals;"color&colon; &num;0000ff&semi;"><strong>for<&sol;strong><&sol;span> loop<&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>for<&sol;strong><&sol;span> loop with item <strong><span style&equals;"color&colon; &num;0000ff&semi;">index<&sol;span><&sol;strong><&sol;li> &NewLine;<li><span class&equals;"s1"><a href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;for-each&period;html"><span class&equals;"s2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">forEach&lpar;&rpar;<&sol;span><&sol;strong><&sol;span><&sol;a><&sol;span><strong><span style&equals;"color&colon; &num;0000ff&semi;"> <&sol;span><&sol;strong>method&period;<&sol;li> &NewLine;<li><strong><span class&equals;"s1" style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;for-each-indexed&period;html"><span class&equals;"s2">forEachIndexed&lpar;&rpar;<&sol;span><&sol;a><&sol;span><&sol;strong> that gives us index and value in each iteration&period;<&sol;li> &NewLine;<li class&equals;"li2"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s1"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;kotlinlang&period;org&sol;api&sol;latest&sol;jvm&sol;stdlib&sol;kotlin&period;collections&sol;-list-iterator&sol;index&period;html"><span class&equals;"s2">ListIterator<&sol;span><&sol;a><&sol;span> <&sol;span><&sol;strong>and a while loop&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre> val numbers &equals; listOf&lt&semi;Int&gt&semi;&lpar;1&comma; 2&comma; 3&rpar;<br &sol;><br &sol;> print&lpar;"print list using for loop&colon; "&rpar;<br &sol;> <strong> <span style&equals;"color&colon; &num;000080&semi;">&sol;&sol; print list using for loop<&sol;span><&sol;strong><br &sol;> <span style&equals;"color&colon; &num;0000ff&semi;"><strong>for<&sol;strong><&sol;span> &lpar;number in numbers&rpar; &lbrace;<br &sol;> print&lpar;"&dollar;number&comma; "&rpar;<br &sol;> &rcub;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; print list using for loop&colon; 1&comma; 2&comma; 3&comma;<&sol;strong> <&sol;span><br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;000080&semi;">print&lpar;"for loop with item index&colon; "&rpar;<&sol;span><&sol;strong><br &sol;> <span style&equals;"color&colon; &num;0000ff&semi;"><strong>for<&sol;strong><&sol;span> &lpar;i in 2 until numbers&period;size&rpar; &lbrace;<br &sol;> print&lpar;"&dollar;&lbrace;numbers&lbrack;i&rsqb;&rcub;&comma; "&rpar;<br &sol;> &rcub;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;for loop with item index&colon; 3&comma; <&sol;strong><&sol;span><br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;000080&semi;">print&lpar;"print list using forEach loop&colon; "&rpar;<&sol;span><&sol;strong><br &sol;> numbers&period;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>forEach<&sol;strong><&sol;span> &lbrace; i -&gt&semi; print&lpar;"&dollar;i&comma; "&rpar; &rcub;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;print list using forEach loop&colon; 1&comma; 2&comma; 3&comma; <&sol;span><&sol;strong><br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;000080&semi;"> &sol;&sol;forEachIndexed&lpar;&rpar; that gives us index and value in each iteration&period;<&sol;span><&sol;strong><br &sol;> println&lpar;"print list using forEachIndexed&lpar;&rpar; &colon; "&rpar;<br &sol;> numbers&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">forEachIndexed<&sol;span><&sol;strong>&lpar;&lbrace; index&comma; i -&gt&semi; println&lpar;"numbers&lbrack;&dollar;index&rsqb; &equals; &dollar;i"&rpar; &rcub;&rpar;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;print list using forEachIndexed&lpar;&rpar; &colon; <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;numbers&lbrack;0&rsqb; &equals; 1<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;numbers&lbrack;1&rsqb; &equals; 2<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;numbers&lbrack;2&rsqb; &equals; 3<&sol;span><&sol;strong><br &sol;><br &sol;> val i1&colon; ListIterator&lt&semi;Int&gt&semi; &equals; numbers&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">listIterator&lpar;&rpar;<&sol;span><&sol;strong><br &sol;><br &sol;><span style&equals;"color&colon; &num;000080&semi;"><strong> println&lpar;"print list using ListIterator in forward direction&colon; "&rpar;<&sol;strong><&sol;span><br &sol;> while &lpar;i1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">hasNext&lpar;&rpar;<&sol;span><&sol;strong>&rpar; &lbrace;<br &sol;> val index &equals; i1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">nextIndex&lpar;&rpar;<&sol;span><&sol;strong><br &sol;> val element &equals; i1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">next&lpar;&rpar;<&sol;span><&sol;strong><br &sol;><br &sol;> println&lpar;"nums&lbrack;&dollar;index&rsqb; &equals; &dollar;element "&rpar;<br &sol;> &rcub;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;print list using ListIterator in forward direction&colon; <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;nums&lbrack;0&rsqb; &equals; 1 <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;nums&lbrack;1&rsqb; &equals; 2 <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;nums&lbrack;2&rsqb; &equals; 3<&sol;span><&sol;strong><br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;000080&semi;"> println&lpar;"print list using ListIterator in backward direction&colon; "&rpar;<&sol;span><&sol;strong><br &sol;> while &lpar;i1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">hasPrevious&lpar;&rpar;<&sol;span><&sol;strong>&rpar; &lbrace;<br &sol;> val index &equals; i1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">previousIndex&lpar;&rpar;<&sol;span><&sol;strong><br &sol;> val element &equals; i1&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">previous&lpar;&rpar;<&sol;span><&sol;strong><br &sol;><br &sol;> println&lpar;"nums&lbrack;&dollar;index&rsqb; &equals; &dollar;element "&rpar;<br &sol;> &rcub;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;print list using ListIterator in backward direction&colon; <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;nums&lbrack;2&rsqb; &equals; 3 <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;nums&lbrack;1&rsqb; &equals; 2 <&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;nums&lbrack;0&rsqb; &equals; 1<&sol;span><&sol;strong><br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;000080&semi;"> &sol;&sol; using ListIterator<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;000080&semi;"> &sol;&sol; print list in forward direction<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;000080&semi;"> &sol;&sol; starting at the specified index<&sol;span><&sol;strong><br &sol;> print&lpar;"print list using ListIterator starting at the specified index&colon; "&rpar;<br &sol;> val it&colon; ListIterator&lt&semi;Int&gt&semi; &equals; numbers&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">listIterator&lpar;2&rpar;<&sol;span><&sol;strong><br &sol;> while &lpar;it&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">hasNext&lpar;&rpar;<&sol;span><&sol;strong>&rpar; &lbrace;<br &sol;> val element &equals; it&period;<strong><span style&equals;"color&colon; &num;0000ff&semi;">next&lpar;&rpar;<&sol;span><&sol;strong><br &sol;> print&lpar;"&dollar;element&comma; "&rpar;<br &sol;> &rcub;<br &sol;> println&lpar;&rpar;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;print list using ListIterator starting at the specified index&colon; 3&comma;<&sol;strong><&sol;span><br &sol;>&rcub;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong><br &sol;><&sol;strong><&sol;span><&sol;pre> &NewLine;<p><&sol;p>&NewLine;

Exit mobile version