Linked List Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're having a great day so far today you're gonna learn about the linked list with me it's gonna be so much fun it will make linked lists a lot simpler for you but first if you're new here my name is Alex Ali I post a Java tutorial on this channel every week so if you might be interested in seeing that then consider subscribing let's start learning about linked lists by just making a Java project we'll call it linkie this DS pro tip do not name projects or classes after the name itself so like if we named it linked list that would confuse the crap out of the programmer why writing because it's also a class name it'll cause a bunch of errors we don't want that so call it something else linkie will add a main method and done basically a linked list is a data structure just like an array a stack or queue a structure your data a linked list looks like this linked lists link link e equals new linked lists import it and there's your linked list it's called link e and let's see what we can do with it you can do a bunch of stuff with a linked list you can add to it you can remove from it you can clear it you can add it to the first position you can add it to the last position you can iterate through it you can see if it's empty it's a very flexible data structure it's called a linked list because each value in the list knows the front and next element which makes it really flexible of a data structure it's super awesome so let's add some stuff to it let's say we've got strings we've got Rob we've got me put Alex in here you've got you I guess what your name is Jose your name is Jose and we are going to print you out let's print out everything in the linked list data structure we've got Rob Alex Jose which is the order that we added so it keeps the order if we want to remove from a linked list we can do this remove boom and it removes from the front we can clear everybody from the list like this clear everyone is gone if this seems a little fast don't worry because all I'm doing is typing the name of the linked list and doing it dot and these are all the methods that that linked lists can do so I'm literally just like picking methods from here I could click it and do one like get first get first let's see what that does it gets the first element but we're still printing the whole thing so obviously it's not gonna show up so let's put that in here instead we'll get the first and that is Rob we can get the last we can do all sorts of stuff to the linked list we can even do integers in here objects we're really just scratching the surface it's good practice we see a bunch of yellow underlines here because it wants us to do the good practice thing and put the type of the elements inside of little alligators here and those underlines go away let's start over and do some numbers so you've got 6 7 th and 1 these are red because it's like well you told us it was a string but it's actually an int and now it's like we can't put primitive types in here Alex it has to be the capital one okay save and run and now it just works with integers so let's try to iterate through these and say we want to get 78 how do we get just 78 and not the entire linked list well we can do that like this gets one at a certain index we know indexes start at 0 so 78 would be one boom and that's how it gets 78 but what if you don't know the index or what if you have a hundred elements in your linked list and you don't want to worry about the exact index well you can iterate through them using an iterator object so we'll just do the same thing let make an iterator instead iterate or pop that in here using Java dot util and instead of making a new iterator we're gonna use the linked list iterator by doing linkie dots iterator boom now I can iterate through it and get the 78 that we want to use the iterator usually throw it in a while loop and you do while the iterator has something next then we'll do stuff and say if that thing if it dot next fit that next is equal to 78 then we will print out and it's saying we need to cast this because this returns an object and this is an integer maybe we can cast it like this it's yeah so I'll try that and we can print we found 78 boom like that and we can run code based on the specific element so that was super quick on how to use the important things of a linked list there's also a few other ones like is empty and there really there's just so many it's super flexible and I do have this other website up here to show important information about linked lists a link to lists can contain duplicate elements it maintains the insertion order as we saw it's non synchronized but you don't have to worry about unless you're doing multiple thread coding linked lists are fast because each element knows about the one in front and next to it so that doesn't need to be any shifting of all the elements and you can use a linked list as a regular list a stack or a queue so this is really just like a quick refresher or introduction on how to use the linked list code and also a high level of what it is I'm sure your teacher expects you to know like all the all the specifics of a linked list like the diagram and the order of everything this way was to make it a little easier on you to start coding with it I hope this is a little helpful for you let me know in the comments while you're using link to us for or if you have to implement one yourself I have a great rest your day and I'll see you in the next video [Music]
Info
Channel: Alex Lee
Views: 118,128
Rating: 4.9097662 out of 5
Keywords: linked list java, linked list, java linked list, linkedlist java, linkedlist, linked list in java, java linkedlist, linked list java tutorial, linked list in java tutorial, alex lee, linked list tutorial
Id: YQQio9BGWgs
Channel Id: undefined
Length: 7min 11sec (431 seconds)
Published: Thu Mar 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.