• 9 minutes ago
Transcript
00:00Next time, let's talk about what we can perform in the area list.
00:04First of all, the operation that we perform is to add an element.
00:08In the area list, how can we add an element in the area list?
00:11The time complexity will be big of 1.
00:15Let's see how it is.
00:16What I did in my main function is a list name.
00:21And what I can store in this integer.
00:25So what I will do?
00:26I will write list.add.
00:29And in this, I will pass the value in which I want to store.
00:33So first of all, I have written the name of my area list.
00:35Then I have written . and then I have called the add function.
00:39This list object will be called for add function.
00:42And in that, I will add value 1 as my integer.
00:45After that, I can add other values.
00:48I will add 2.
00:49I will add 3.
00:52Once again, I will add 4.
00:55So this way, I have added 4 different numbers.
00:58And when I add one number, which has a time complexity,
01:02that is big of 1.
01:03That means, when I add one number to a list,
01:06I feel big of 1 time, or constant time.
01:09After adding all the data, how can I see my list?
01:12I can print it.
01:12I will simply write system.out.println.
01:16And here I will write my list name.
01:18So let's run this one.
01:20So my list is 1, 2, 3 and 4 stored.
01:25If I will add another number here, which is list.add5,
01:29then what will happen?
01:30Now there will be another number.
01:32So in my list, there will be 5 elements.
01:35The second operation we will talk about is to get element.
01:38That means, I have added element.
01:41But if I have to know that I have to know
01:43that which element is i is equal to 2 is stored.
01:46So how do I get it?
01:47We will use the get function.
01:50So we also write code for get function.
01:53Here I have printed my list.
01:56I have printed my list, now what will I do?
01:58Get operation perform.
02:01I will write list.get.
02:03In the get function, I will pass my index,
02:06which I need index value,
02:08if I need index number 2 value.
02:10So in this way, I have done list.get.
02:12Here I can write int element is equal to list.get.
02:16And I will print my element.
02:20Let's save and now run.
02:25So 1, 2, 3, 4, 5 is my list.
02:27There is index number 2 element 3.
02:29So that is my print.
02:31So we perform get operation in this way.
02:33In Java, the get operation is also performed in constant time.
02:36Like in Array, we were looking at the constant time.
02:41In Array list, get operation is performed in constant time.
02:46Next operation which we will talk about is remove element.
02:49This means once I have added or added element.
02:53Now, if I go to an index and remove the stored element in that index,
02:57what will I do to remove?
02:58What will I do to remove?
02:59To delete or remove an element,
03:01there is also a function in the area list.
03:04If I want to delete, then what will I do?
03:07I will call list.remove function.
03:10And in this remove function, I will pass the index which I want to delete.
03:15Let's say I want to delete the stored element in second index.
03:18And after delete, I will print my list again.
03:23Let's do it. Comment out.
03:25So first, if I store my Array list 1, 2, 3, 4, 5, then I have stored value in index
03:30number 2.
03:31I have deleted the stored value in 3.
03:33So now I have stored elements in my Array list.
03:351, 2, 4, and 5.
03:37So I can remove values in any other index.
03:40Now, this is my delete operation.
03:42I feel linear time.
03:44I feel linear time because first, we will search what is stored element in that index.
03:49Then I will remove the element from that index and then I will remove the element from there.
03:55So there will be a little resizing internally in Java.
03:58Next operation which we will talk about is setElementAtIndex.
04:02So if I already have stored in my Array list 1, 2, 3, 4, and 5.
04:08And today I want to make my index i is equal to 2.
04:11Whatever value stored is stored, it will be stored there.
04:13It will be stored there, it will be stored there.
04:14That means, I will remove 3 and I will store 10 here.
04:18So I will use this function, setElementAtIndex.
04:22Let's write code for this.
04:24SetOperationPerform.
04:25I have to write your list name.
04:27Then I will call the .set function.
04:30.set.
04:30First, I will pass my index.
04:33To.
04:34After that, I will pass the new value that should be stored on the index.
04:38Which is 10.
04:41Then I will do comment out and save it.
04:43And once I have setElement.
04:45So now I am going to print my list.
04:49In the beginning, my list was 1, 2, 3, 4, 5.
04:52What did I do?
04:52I have stored in index number 2 to element 10.
04:55So now, where the index number 2 is.
04:57I have element number 10.
05:00This setOperation also performs in linear time.
05:03Because we have to go to that index.
05:05After that, we have to change the element.
05:08The next operation we will talk about is containsElement.
05:12The containsElement is checking whether any element exists in my array or not.
05:18If I want to check in this array, what does 4 exist?
05:22If it exists, I will get true.
05:24And if I want to check that it exists, then I will get false.
05:28Because 12 does not exist in this array list.
05:31So we will perform the operation for contains.
05:34First of all, I will check what list.contains.
05:38Now I will check what element I want to check.
05:40I want to check in this array list.
05:42I want to check in this array list.
05:43I want to check in this array list.
05:46I want to check in this array list.contains for my number 11.
05:51I want to check in this array list.
05:52Comment out.
05:53Save.
05:55Run.
05:57First of all, I have to print true because my list exists in my list.
06:01After that, I have to print false because I don't exist in my array list.
06:05In which 11 exists in my list, it doesn't exist in my list.
06:06So whenever the number exists, it will be true.
06:09And whenever the number exists, it will be false.
06:12Now I have performed the add function.
06:15Besides that, there is another different type of add function in Java.
06:18In which we can perform the array list, I can directly add value to any index.
06:24So I will write list.add.
06:26Now I will write the name of my index.
06:27So if I want to add value to index number 1, then I will add value to 9.
06:40After adding value to index number 1, I will print the list.
06:43I will also print comment out.
06:46Save.
06:47We are running this part.
06:51So what made my list?
06:53In the beginning, I knew that my list was going to be 1, 2, 3, 4, 5.
06:55But what did I do?
06:58I added 9 to index number 1.
06:59So here, I added 9 to index number 1.
07:01What did the rest of the elements do?
07:03The rest of the elements are able to get back.
07:06So that's why the add operation is not performed in constant time.
07:10It seems linear time.
07:12It seems linear time.
07:13In which we add some particular element to a particular index,
07:16it seems to be bigger of any linear time.
07:19But the normal add operation is my constant time.
07:22Now, a special thing that we have noticed about the array list,
07:25is that we can access the value of the array.
07:30We can access the value of the array directly to the array of i.
07:32But here, we don't put any square brackets in the array list.
07:36For the list, we use only the get method and the get function.
07:40So these are different distinctions between arrays and array list.
07:43But their properties are almost the same.
07:46That means, all the functions that we perform in the array list,
07:49we can also perform in the array list.
07:52In fact, when you participate in many contests,
07:54we don't give arrays,
07:56but there are array lists.
07:58Because array lists are dynamic size.
08:00Based on the problem,
08:01we can increase the size of the array list.
08:02for the reason,
08:03we can increase the value of the array,
08:03for by the array list.
08:03We can increase the value of the array list.
08:04Therefore, the option of the array list will take that into place.
08:04What can we do?