declare

#10.2 Kotlin LIST and ARRAYLIST tutorial. Mutable and Immutable Collections for Android PART-2



Sharing buttons:

hi reassuring Siddarth once again

welcome to the next video of this module

of collections now in this module we

will check out the immutable collection

and mutable collection so first of all

in this video we will check out what

does list stands for so in case of lists

we have the immutable list and also we

have mutable list so we will check out

one by one what does the immutable and

mutable is stands for and what are these

functions such as list of ArrayList

ArrayList of and mutable list of stands

for so let's check out one by one and

slowly inside the IntelliJ IDE so in the

previous video we checked out what our

arrays in case of Kotlin now the demerit

of array is that it has a fixed size

defined so we cannot change it later so

here what I will do is I will simply

modify my code so here I will simply

delete all these code from here now to

define a list we need to simply use the

VAD or Val keyword and then simply use

list equal to use the list of keyword

right and now here as a template let us

define string and now as a value you can

simply define the content of the list

such as Yogi Manmohan and at last

watch pay so here I have simply defined

the immutable list by using the keyword

of list of now this is the type of list

that we are having so this list variable

will only contain the string values or

you can say the array of string right so

here as a value I have Yogi Manmohan and

watch pay we are having three string

values right now as per this slide here

this list of keyword only creates the

immutable list right so let me add a

comment line here that is this list is

actually immutable in nature and also it

has fixed size and most importantly we

can only read from this list that is

later on we cannot modify the list or we

cannot write anything extra to

list that is once the content of the

list has been defined it cannot be

changed what I am trying to say is you

cannot use such as lists of let's say

index to equal to let's say something

right

ABCD right you cannot do it because the

Cortlandt does not allow you to do it

because the list is actually immutable

in nature you cannot modify it later you

can only perform the read operation now

what do I mean by the read operation is

you can only print out the values

present inside the list using the loops

that we saw in the previous video right

and one more thing is common for this

list compared to the arrays is that the

lists also have the sequential index

that is this you he has the index of

when this element has the index of sorry

this yogi has the index of zero manmohan

has the index of one and this watch bay

has the index of two and so on it will

continue if we have further elements

right so as of now let us remove the

elements of integer and in case you want

to enter the int value so you can simply

define the enter type and simply replace

all these value by integer values now

same applies to the float or let's say

boolean or let's say string right and

now let us print all these values such

as I place my array by list and then

here let us replace my array by list dot

size minus one right and here again use

list of the index now apart from this

list index of index you can simply use

list dot get function and simply pass

the index so this loop is actually using

the index of the element objects to

print out the values right and instead

of lists of index you can simply use you

can also use list dot get index so as of

now let us run the code and let's see

what happens so here we get yogi

manmohan and watch pay and again we get

yogi manmohan and watch pay right so our

codes are

working perfectly fine now suppose later

on you want to add the fourth element

that is here you want to add one more

element so for that this list off is

actually immutable in nature so the

solution for this is that you have to

use the mutable list so here from the

slide you can check out the mutable list

we have the error list option ArrayList

of option mutable list of option so

either of these three you can use as per

your choice so as of now let us replace

this list off by let us say mutable list

off right so this now makes our list

immutable in nature so let me modify the

comment line so this new table list off

is actually mutable in nature and it has

no fixed size you can add as many

elements as you want to and also you can

perform the add or remove operations

such as read or write operations as

discussed because it is immutable in

nature right so inside the list what you

can do is you can simply use you can

simply add more elements such as list

dot let's say add so here let me add

yogi so if I add yogi here then you can

remove yogi from here right similarly

you can add more elements after the

declaration of the mutable list so since

we have added the manmohan and watch

page here so let us remove these

elements from here so it can be blanked

so this initialization of the list

variable can be blanked right now after

this

suppose if I run the code right now it

is simply going to print out these three

elements in the output console as

expected yogi manmohan in watch Bay now

later on suppose you want to perform

more operation on this mutable list so

for that what you will do is you can

simply use as many methods as you want

to and these methods are actually

predefined such as add method replace

all method remove the element at a

specific index set the element at a

specific index get the size of the list

and get a specific element at a

particular index and so on right so as

of now what I will do I will simply

remove the element of let's say my

movement right and instead of manmohan

that is present at the index of let's

say 1 so here I have simply defined the

index of the element this manmohan is

present at the index of when so instead

of manmohan I want to add Moody so for

that what I will do list dot add provide

the index of let's say when add the

index of 1 simply add Moody instead of

manmohan let us now run the code and

let's see if it happens or not so here

in the output console we have yogi moody

and wash face so the manmohan has been

replaced by moody right now instead of

this using the remove and add method

that is predefined we can also use lists

at the index of when equal to let's say

moody right so this list at the index of

when simply replace whatever is present

at the index often by Moody so let us

now run the code so here we get yogi

moody and wash Bay again so instead of

using these two lines of code we have

only one line of code that performs the

same operation as done by these two

lines of code altogether and now at the

end instead of mutable list we can also

have let's say array list of strings and

also instead of this you can also use

array lists now to be specific in all

the cases we are actually using the

mutable array list such as here we have

the ArrayList of string here we have

ArrayList of method that simply returns

the ArrayList right and here again

mutable list of actually returns the

ArrayList object so if you define the

type let us say alt-enter specify type

explicitly you will find the mutable

list which is the more general form and

here again for

this alt-enter you will find ArrayList

of string and here again specify type

explicitly you will find ArrayList of

string now this mutable list is actually

implemented by the ArrayList of string

so all these methods such as mutable

list of ArrayList of an ArrayList

are all same so you can use either of

these three just for the sake of your

more information I just showed you the

explicit declaration

so let us revert it back and here let me

comment these two lines of code and here

ArrayList of string is again going to

work perfectly fine let us run the code

and let's see if we are getting the same

output or not yes yogi move the and

watch by again in the output console so

remember at the end we have list of

method that simply returns the immutable

list and here we have the mutable list

such as ArrayList

ArrayList of and mutable list stop you

can use either of these three in your

code so in the next two videos we will

check out math and sets in Kotlin so

this shrinks without signing off thanks

for watching and have a good day thank

you