OK let's not write them off on a list. 2
So in the last lesson bilabiate brought another list is an buter looked at few methods from the list 3
interface and also in this class. 4
So in this demo we'll be looking at all of those methods and we You'll also look at a few methods from 5
the collection interface which is the root interface. 6
So for this demo I have created this new package called Comdex semantics credit collections and I have 7
also created a new class within that package called list them all. 8
And within the this demo class we have this method called us our deadliest demo which we are going to 9
fill in this in this demo. 10
So we are going to write some code here and it's a pirate's topic matter that is invoked from the main 11
method. 12
So we're going to write some code. 13
And we also have some code that has already been written. 14
So that is done in the interest of time. 15
So when we get here we'll be just looking at the court and we'll just run the program. 16
But before doing that we will write some code here. 17
So let's go ahead and first let's create an idealist 18
. 19
OK. 20
He had the name of the artlessness list one and here that referenced this collection which is an interface 21
and we have created an Adela's OK so we should always try to program plan interface. 22
We know that because if you do that we can easily change the implementation. 23
So if we want to use linguist all we need to do is change this to a linked list and collection interface 24
since it is the root interface. 25
It gives us the maximum generality. 26
And we know that these facts are related to generics. 27
So let's not worry about it. 28
But for now it would suffice to understand that since we have integer here in between the stocks it 29
means that it's an other list of India. 30
So this list this particular analyst can store only integers. 31
So if we try to add any other object other than an integer then we get a compiler error. 32
Now if you want to Ardelia's to store to store on these strings then we simply have to change just a 33
string. 34
And if it needs to be double we just change it to double. 35
So in that sense it is genetic. 36
So the idealist can store anything any any particular object and all we need to do is do this through 37
of genetics notation we need to indicate the kind of objects that we want to store. 38
And that's the reason why it is Carlist genetics. 39
It makes our Clausius genetic to distort anything anything that we want. 40
So that's about it for now with regard to genetics. 41
And we will discuss it when we get there. 42
But all that he does. 43
So let's just go ahead and add few elements to do this or at least 44
sort of. 45
I do. 46
Let's add a duplicate here. 47
And let's And not since honorless can store both duplicates as well as null. 48
Let's go ahead and print the elements. 49
No the mirror on it. 50
So as you can see they're distorting all these elements and it can store duplicate as well as other 51
values. 52
Now let's go ahead and remove the element at that third index. 53
This is 0 1 2 and 3 it needs to remove this one. 54
So let's go ahead and say at least one board read more. 55
So that element or that third position has been removed. 56
Now the set list. 57
So let's do this one more time. 58
In that case it has to remove the value knowledge since it is at the third position in 0 1 2 and 3. 59
Now as you can see it did not remove nutten but it removed the elementary. 60
So the reason for that is because it is due to metal binding. 61
If you recall our lesson on method binding we said that there is this method signature binding which 62
happens at compile time itself. 63
So at compile time the compiler is looking at the reference type which is collection in this case and 64
collection has this method called remote island. 65
It has this parameter object. 66
So at compile time the compiler will try to find an exact match a method with which has the exact parameter 67
which is an NPR remote off. 68
And but the collection interface does not have that method. 69
The removal of a method is present only in the list interface. 70
Ok but not in the collection interface. 71
But then compiler sees that it did not find the exact match and method with the exact matching signature 72
but it found a method having compatible signature. 73
OK so in that case in the bike code it's going to write that the method that needs to be invoked should 74
have the signature. 75
OK I don't mind the JVM. 76
Well really try to find the most specific method having the signature which is also present in the list 77
interface in other districts. 78
So if you don't recall just go back to the coalescent on method binding and you can under the topic 79
on the method signature binding will explain this. 80
So. 81
So that's about it. 82
So that's the reason why. 83
So actually even when we invoke up here in the first instance it was not removing the element of purpose 84
. 85
But it was actually removing the element 3 itself. 86
OK. 87
And the same happened here it remove are the elementary. 88
Now if you want to make make it are the element at the Toti next door be removed then we need to change 89
this list. 90
In fact let's change it to list and that's because the the methods that we are going to invoke math 91
. 92
Next we are going to write next are specific to our position specific methods. 93
So we each have an index number as the parameter on disposition specific methods are specified only 94
and the list interface. 95
Now if I run this you can see the non-God to look at which means that it didn't work the method. 96
This particular mother more off and. 97
OK. 98
So that's that's the metric signature of a lot of people in order to understand that how them at their 99
signature binding works. 100
So you can go back and refer to that particular lecture and object oriented programming. 101
Now let's go ahead and write some positions specific methods. 102
Let's add an element at zero. 103
So you have a new element here added at zero. 104
It's. 105
So that's the I'd mutter that an index here is zero. 106
Now let's try the set matter. 107
So let's set the element of zero position with just 10 to nine. 108
There you go so nine has been added and this method would return the previous elements stored at that 109
position which would be then. 110
OK so these are the new elements that we have 9 1 2 and 3. 111
So those are the items segment. 112
Now let's look at some bulk operations. 113
So we know that the collection interface are some bulk methods. 114
So here it is. 115
So let me uncomment this. 116
So it's already there. 117
So here we are creating a new list. 118
OK. 119
In New Collection. 120
It's called us let's do I've got two elements 9 and three iron from list 1. 121
We are woking we are invoking the remove all method on list 1 and we are passing listed as the input 122
barometer input argument. 123
So we want to remote If list one has both these elements. 124
We want to remove them from list 1. 125
So let me just run this. 126
So we only lost 100 only 9 1 2 and 3. 127
So list 2 had 9 and 3. 128
So we have removed the bench. 129
So we are left with only one and 2. 130
Now go let's go. 131
Iron I have one Boulis too. 132
So let's to had already had nine and three. 133
Let's also add one to it and let's. 134
In order to retain All-Met there. 135
So we'll have more on NEXT is reading all matter. 136
Here it is written on. 137
So read in. 138
All well written all the elements present and list do on that word remove all the remaining elements 139
. 140
OK so list one right now has only 1 1 and 2 and a list to list one has one undo unless Drew has a 93 141
on one. 142
So the only common element is 1. 143
So it will retain that and really move to. 144
Here it is. 145
So it retained one deleted element. 146
So we are done with the remote all undereating on. 147
Let's look at at all. 148
So let's just go ahead and simply add the elements off list to do list 1 149
because you can see the three elements mind three and one which were part of list 2 are ordered to list 150
1. 151
So that's the ideal method. 152
And let's also do this. 153
So this is just pure setting the value. 154
Enlist one element at indexable they do is reset to two which means that this tree gets changed to sort 155
of try on it once again. 156
And here it is here. 157
The set met there simply returns the search the value at that particular index to do under the previous 158
element the previous element of the next Wall Street. 159
So it's district running back. 160
So you're just printing out that value. 161
So those are the bulk operations. 162
And this was just a set operation. 163
Now let's go ahead and look at some such methods. 164
Here we are so the first one as the contains mentored and we are just checking if the element one is 165
present and the last one second is index off which gives the first utterance of the element of the next 166
number of the first occurrence of this particular element if it is present. 167
If not it would read and a minus one third one is lost and next off which would give the last index 168
off the goodness of this element. 169
So let's run this. 170
Now as you can see the element 1 is present and the last one and the last one. 171
So it returns a true here on the index position. 172
When we do index off it is zero. 173
But when we do the last index off it gives three because the last element here is one. 174
OK. 175
So it gives up in next three. 176
So those are the search operations. 177
Now we also discussed a broader range view the sublist method. 178
So let's also try that. 179
So range rules just because that's part of the list interface listen. 180
So here we are calling the sublist matter and we are saying we are asking for a sublist and the element 181
indexes are the lets us do the two indexes three so it will return only one element Element 2. 182
Because this is exclusive. 183
So the the name of this list is list 3. 184
That's the sublist. 185
We said that the list 3 when we do a sublist. 186
This particular list is backed up by the origin of that original idlest which is a list 1. 187
So which means that any changes that we make to this new list will be reflected in that list. 188
So let's go ahead and run this. 189
Here it is. 190
So in the list 3 Initially it had only it had only the element at index position to know we are setting 191
it to 6 and we are printing list one here. 192
So as you can see when we have said the value industry it also got to reflect it and list one on that 193
particular value at position you got updated. 194
OK. 195
Now we are updating the value we are. 196
I think a value to list three. 197
Now when we do that it also gets reflected in list 1. 198
So we're adding it. 199
The position zero but that position zero is actually the index position to and list one. 200
So as you can see the elements 7 what I did on the other two elements got shifted to right. 201
So any change that you'll make in this new list will be reflected in that and that put you in the back 202
of a list which is the original list which just listed them. 203
So we have made some changes in the new order list in the sublist. 204
And that got reflected here. 205
No need to make some change in the original list and see that it gets reflected in the industry. 206
So here we are making a change. 207
We are setting we are resetting the value of the element at position to 8 under to see if it gets reflected 208
in industry. 209
So we are printing a history here. 210
And as you can see the history the value at zero at the position index 0 got changed to 8. 211
So the change was made in this one and it got reflected in history. 212
Now let's see if we can I add an element and last one on whether it gets reflected in industry. 213
So we're adding an element at index position zero in the last one and it doesn't. 214
So we are we have we just tried to add an element and to list one. 215
But when we tried to prend the elements of history it did not look. 216
So we can only make non-structural changes in a just one. 217
Only then we can access the elements in history. 218
Now let's say we made a structural change here a structural changes adding an element or deleting an 219
element and when we do that then if we try to do when we try to access any of them at industry then 220
we get an exception under System and modification exception. 221
So anything we can do as analyst one we can or we can set something in that case it is fine but if we 222
make any structural change in the back array then the new order the sublist will throw an exception 223
when we try to access any of its methods. 224
So that's the thing. 225
But if we make any changes in the in the sublist then it is fine. 226
We can I'd also be we can make structural changes. 227
Also industry then that gets reflected in last one. 228
Okay. 229
So that's the thing. 230
Now let me uncommon this. 231
I think we are just printing the same elements not as it it through the elements of the of the list 232
. 233
So we are simply having a For Each loop here. 234
But let me just Carmen this note. 235
My read on it. 236
So as you can see we are just iterating through the elements and I'm just printing all these five elements 237
and the last one. 238
Now let's see if element is nine. 239
So the 11 element minus here the second element. 240
Let's say we want to remove that element from the last one. 241
OK. 242
So we are iterating through last one. 243
But if the element current element happens to be 9 we want to read more and see what happens. 244
As you can see we get an exception here. 245
It's a concussion modification exception. 246
So and So that element 1 got printed here here and then element 9. 247
The second element was 9. 248
It also got printed but if element is nine we are trying to remove it and then it throws in concurrent 249
modification exception. 250
So the thing is when we are iterating if we want to remove a particular element during the iteration 251
then we cannot do it using a for each statement. 252
And for that we need to use it for that matter which is the subject of next lesson. 253
OK. 254
Using an editor to. 255
We can do that. 256
And so that we will see in the next lesson. 257
So thank you. 258
And this code is available in the resources section for download and you can download our and you can 259
run it on happy coding. 260
And thank you.