 Hey everybody, this is Brian and welcome to the 24th Java video how to request from hard hit of 1,000 Who wants to see linkless or trees and Java well to understand linkless and trees? You first have to kind of go back and Do a little bit of research here. You have to understand what an interface is and you have to understand what generics are So first thing what is an interface? An interface is a contract between two objects for example think of your car key if you have a vehicle Or if you don't have a vehicle your bus pass that allows you to Turn that vehicle on if you have a car or to get in the bus It's a contract saying you have to have a certain thing in order to do something else So if you're going to implement a list you have to adhere to the list interface and you can look that up online There's a lot of verbiage out here, but it says here's the things that must be in there like add add all clear Contains equals etc etc Looks like a lot and it is but The developers have already done this for you. For example the array list is the in an implementation of a list interface It's a very simple implementation Now what's the difference between a list and an array? Well an array if you remember right you have to define an upper limit a list You don't have to do that you can just keep adding and adding to your heart's content makes it much simpler to work with now a lot of college classes and online training make these Classes much more difficult than I believe they really need to be so we're going to go through these really quickly I think you're smart enough to really catch on so bear with me Let's just jump right in All right. The first thing we need to do is go import the Java dot util namespace and and If you want to kind of explore the namespace you actually see that here's the array list and then there is a whole slew of Other classes in here. You'll get really really familiar with the util namespace, but for now We're just going to use the array list And then the second thing you want to do is actually let's make an array list. So let's go array list And when you select that from intelligence, you know she get this Bracket he bracket what in the world is that? Well, that's the second thing you need to understand. It's called a generic well a generic in case you're scratching your head wondering what it is is simply defining a type For example, if we said this array list can take strings We'd have to make an array string list and then what if I wanted to do integers You have to know to make an array into list and what if I made a class called dog You'd have to make an array dog list and on and on and on it gets a huge headache So what they did is they came up the concept of generics, which means when you create an array list or Something that uses the list interface. You have to define the type that it's going to use So we're creating an array list we're specifying the type This is called a generic whenever you see these brackets and here's the name. We're going to call it nums and equal new Array list and you have to also enter the Brackets at the end because this is the default constructor. Remember our conversation on classes it looks kind of funny that you have to declare the type twice and It is kind of funny. There's a whole big conversation We could get into about typecast and all that but we're not going to do it We're going to keep it very simple for this tutorial Once again, you're making an array list which follows the list interface which takes a generic We're saying the generic's going to be string. So basically what we're saying is we have an array list of strings called num Let's just copy that variable and now we have nums So the second thing we need to do is add items to it. So we'll say add string So we can add the word hello Now you might be wondering I'm still kind of fuzzy on this whole generics thing Show me what you mean. So let's do that So actually plug in the integer type and Notice how add suddenly starts complaining eat this method add integer notice how it suddenly wants an integer instead of a string Now you really see the power of the list interface. We'll just say 33 That's how generics work. You can use the same class with different types of data So let's flip back to strings just because I like working with strings Let's copy this Paste it here and see now once again add is complaining. Well, it takes a string not an integer. So Let's actually add the word one And through the magic of copy and paste my favorite thing in the whole world We're gonna add a couple of these and we'll say two and three So that's it. That's how you add them Now, how do you print these out? Well, there's a real simple way you can go system That print Oops, I'm sorry used to another language here system out that print And I'll say print line and you can actually just print the variable out And let's run this and see what happens. You know, it's how in brackets. It says one two three What if you want to access the individual elements here? Let's do that We'll say for And we're going to use the for each construct because it's very easy to work with And if you don't know what that is that is the for each Basically you're saying for each string in nums. That's what this little semicolon means and you're just going to loop through here We're going to say int i equal nums index of and what we're going to do is we're going to get the index of that variable s So we're going to say for each string in nums one two three. We're going to loop through and we're going to get the index What's the index the index is the position within this list? It's all going to become very clear really fast here system out that print line whoops And we are just going to say I equals And we just want to print out the s. So what we want to do is we want to see the index And then just an equal sign so we know where we're at and then the s variable which is going to be the actual string Save your work. Let's run it sure enough zero equal one one equal two two equal three now List interface is zero based just like an array that you used to and that's how easy it is Now if you want to remove things you can actually just say you know nums dot Whoops. Oh, come on There we go and tell us senses screw it around on me You can remove and you see this int index. Well, it's looking for the position you want to remove So let's remove one which is actually the second position. Remember, this is zero based So let's just put some comments in here zero one And two so we're going to remove one which is actually this guy right here Because this is in the one position So when we run this See how it goes zero one one three. We removed the one position Pretty simple. I know it's a little confusing first walking into it I recommend you practice a few times and really get used to interfaces and generics because they are very popular not just in Java but in other languages as well such as Visual basics C sharp and C++ and a few others that I won't name. All right. This is Brian Thank you for watching. I hope you found this video educational entertaining and stay tuned for more