 Hello everyone. In this video, we are going to learn Arialist in Collections, okay? I am Professor Dattadri Gandmal from Vulture Institute of Technology, Sallapur. Let us start with this video. What will be the learning outcome in this particular video? Students will be able to describe Arialist along with various methods. They are also able to write a program using Arialist. That's an important part of this video. You will be able to write your own program. Now coming to description of Arialist, these are some of the important points in Arialist that need to be noted. First, Arialist can contain duplicate elements. Duplicate elements in Arialist is allowed. Let's say you are entering Sachin twice into an Arialist, it's allowed, okay? And it can maintain the insertion order. It is going to maintain the insertion. Like first you have entered Sachin, then you have entered Raul. So first Sachin will be stored, then Raul will be stored. Likewise, the sequence of insertion is taken care of. And it is non-synchronized. So Arialist is also non-synchronized. And also in Arialist, random access can be done, okay? If I want to access a particular location in Arialist, that can be done. Manipulation is a bit slower in Arialist, but that can be overcome in Linklist because we need to have shifting kind of things, okay? Linklist doesn't have any shifting. In Arialist, we need to shift all the elements. That is taken care by Java, but still time is going to be killed in that particular case. And one more important thing, Arialist uses a dynamic array, okay? That means its size is going to get increased and decreased as per the need. So they are dynamic in nature, which takes place at runtime. How you create an Arialist? These are the steps that need to be taken care of. It can be constructed like this. Arialist AL equal to new Arialist or it can be created with some capacity. Arialist AL equal to new Arialist 20. But capacity is not going to play an important role over here. Let's say you know like, okay, my Arialist won't be more than 20 elements. In that case, you can initiate it, but it is immaterial, okay? Even you give it, you doesn't give it, Java will take care of that. Collections will take care of that. Arialist by passing some another collections into it. So it can also be done. Arialist AL equal to new Arialist and passing some collections, some other collections object over here. You can pass the object directly. So whatever collections you have in this some collection object will be directly copied into this collection object AL. Everything will be copied. Okay? Now let us start with a simple program in Eclipse on Arialist. So we'll come to Eclipse. Now how to start with Eclipse? File, new, Java project. Okay, here we are going to give it as collection demo, okay? And just finish it. Now in collection demo, explore this package on a source, right click, go to new, class file. Here in class file, you're going to name the class file. Well let us give it as array demo, okay? You want public study requirement? Yes, I want. You can click over here and finish it. So this code will come by default. And as I said, Arialist or all collections fall under the package util, java.util, okay? Here in Eclipse, the editor itself will take care of that. Arialist AL equal to new, Arialist, okay? Here it is giving me this red line means there is some error. You click here, okay? It will tell you how it can be resolved. Import Arialist, import Arialist. Arialist has been important. That's how easy it is, okay? If you want for a specific case, this Arialist, you can even do that. So here if I can do that string, so this becomes generic and non-generic type. So Arialist now is of, let me close this, okay? Let me minimize this, okay? Arialist AL equal to new Arialist. For what type of data you're going to store here? String data you're going to store over here. How you're going to add the elements? AL.add. AL.add is going to add the element, okay? Let's say I want to add Sachin. Sachin will be added to my Arialist. AL.add. So this method will come in Eclipse. It will give you the possible method that can be used. AL.add, let's say Zahir. I'll take one more AL.add. You can take any number of elements over here. We'll take Virat. So now four elements have been added to my Arialist. Oh, sorry. It should be add, not add all, okay? Now, if I want to display, I want to check whether these elements are stored properly or not. I can use a for loop, for each loop. I'll use a for each loop in I in which AL.add. In I in AL is going to display all the elements of AL. AL is of type string, so I cannot take your int. It should be string. Therefore, it is giving me an error. int I in, or we can take int S in AL, okay? It says out control tab, sorry, control space. You'll get this system.out.println. Here I want to print what? I want to print all the elements of my Arialist, which is S. So this for each loop is going to run incrementally as I explained in my previous video. Okay, let us see by executing this. You can click on this button. You'll be getting this output. Sachin Raul, Zahir Virat. Here you can note that everything, whatever output you are getting is in the order, the way you have inserted it. So the insertion order and the output order is same. That means in Arialist, everything will be inserted in that particular order only. There is one more way of displaying elements of Arialist. Okay, what is that way? We have an iterator iterator for string it equal to AL.iterator. Okay, then we can use a while loop while it.asnext, we are going to run a while loop. While it.asnext says out it.next element, print the next element. I'll tell you how exactly this while loop is going to work. Let us try to run it. So again you are getting Sachin Raul, Zahir. Okay, let me remove this part for loop, for each loop. Because it will create an ambiguity over here. Now here, how iterator is going to work? What is the difference between for each and iterator? In iterator, you create an object of iterator. Iterator is going to work like similar to a for loop. Okay, it is going to move its position one by one by the methods which you are using. Iterator as the name says it is going to iterate in your collection. It is going to iterate. Okay, now iterator of what type of type string because I have created a type string. AL.iterator on what collection on AL object collection, therefore it is AL.iterator. Now, once this iterator starts pointing to this array location, this array list AL, then I can use methods inside iterator to process it or to display it. Mostly iterator are used for displaying purpose. Therefore, there is a method called as as next. As next is as the name says it is going to return a boolean value, true or false. High t dot as next, agar next element, raha. Take if there is a next element, then it is going to return true. If there is no next element, it is going to return false. Whenever it is false, it is going to come out of this value. Okay, so here inside it system.out.println, what we are going to print, how we are going to move to the next location, it is it.next. So, first iterator is going to point to Sachin, it.next, Sachin will be printed. Okay, then it.next, Raul will be printed as next. Yes, it is there. Zair will be printed as next. Yes, it is there. Virat will be printed after that as next. After Virat, there is no element. So, here while loop is going to get a false statement. Once it is false, it is going to come out of this. So, this is how iterator is going to work in case of collections. Fine. So, you can see if I execute it once again, you will get the output. Sachin, Raul, Zair, that's it. If you want to remove a particular element, you can remove it. AL.remove. AL.remove, I can give an index number or I can give the element also. I want to remove Zair. I have to keep all the batsmen into my team. So, run it once again. Okay. No, there is no Zair over here. Okay. If you want to run it once again, if you want to remove, say, check, you can do it after displaying also. I will remove from here and we will have iterator once again after this removing. This time let's say, let us say we have IT1, IT1, IT1. Okay. Run it. We have Sachin, Raul, Zair, Virat. First time. Second time we have removed Zair. Therefore, it is Sachin, Raul and Virat only. Fine. Now, coming to this part. So, how many ways we can initiate collections? You can pause here and rewind the video and check it. The answer is three ways. Fine. These are various methods available in ArrayList. Likewise, I have used ADD, CLEAR. Okay. E is empty. Contains. There are way in number of methods available in ArrayList. You can pause here and take note of this particular slide. These are some of the references that I have used. Okay. Even if you come here, even if you do AL dot, you can see all the methods which are available in collections. These are all methods you can use. Okay. Thank you.