‫Welcome back.
‫We learned about the immutable lists.
‫Now let's learn about the mutable list.
‫A mutable list is a collection that allows you to modify its elements after it's created.
‫You can add, remove, or update elements within a mutable list.
‫Mutable lists are part of Kotlin's collection framework and are created using functions like mutable.
‫List of and array list of.
‫These lists are used when you need to work with a collection of data that can change over time.
‫I'll start creating my first mutable list.
‫I'll name it as vegetables equals to mutable.
‫List of function.
‫The mutable list of function is used to create a mutable list.
‫In Kotlin, this function takes var arg parameter, allowing you to specify the initial elements of
‫the of the list.
‫Here you can omit this.
‫I can define string, the generic type of this mutable list, or if you specify the elements here.
‫So Kotlin will detect the type of this mutable list.
‫There is no need for the string generic type here.
‫I'll start with potato and broccoli.
‫In this example, the vegetables list, which is a mutable list, can be modified.
‫You can add, remove or update elements in this list.
‫For example, I'll start with the add elements.
‫I specify the mutable list dot add function.
‫The add function is used to append the elements to the vegetables mutable list.
‫I'll add carrots, so this function will append carrots to this mutable list, and it will place carrots
‫at the end of this list.
‫After the broccoli you can remove elements so vegetables dot remove.
‫You can remove at you specify the index remove function.
‫You specify the element remove first remove last remove if remove remove all, remove first or null.
‫And there are a lot of removal functions here.
‫I'll use remove at and.
‫Here I specify the index zero.
‫I'm referring to this potato.
‫Let me print all elements of this vegetables.
‫So here.
‫Specify vegetables and I need to print the item which is the vegetables.
‫Run the program.
‫You can see tomato, broccoli and carrots.
‫We added carrots after broccoli and we removed the potato, which is at index zero.
‫Vegetables can specify this angled bracket.
‫I need to specify the index of the element that I need to update.
‫I'll update tomato.
‫It's at index zero.
‫Because this is removed tomato becomes the first element.
‫So here vegetables at index zero after the removal.
‫Or you can comment this and specify the vegetables at index one.
‫Vegetables at index one.
‫I'm referring for tomato I replaced it with garlic.
‫Let me run the program.
‫And this is our list starts with potato and tomato is replaced with garlic.
‫Also, you can create immutable lists with ArrayList of functions, for example.
‫I'll create another mutable list here val colors equals to array list of and.
‫Here you can specify the type or.
‫Whenever you add new elements it will detect the type.
‫Here the error is gone because I specified the string double quotations red green, blue.
‫Also, you can apply the addition removal and the update functionalities to this array.
‫List of colors dot add I'll add purple removing elements I'll remove colors.
‫Dot remove element zero I'll remove red.
‫Remove at and you can update the elements colors at index one equals to yellow.
‫And I need to print all elements of the list for item in colors.
‫For every item in colors, go and print this item.
‫Run the program and here we go.
‫This is our vegetables list and this is our colors list.
‫I created a mutable list with the help of array, list of function and mutable list of function.