Data Structures & Algorithms Basics
Transcript
Hi guys! Mosh here! Today, we're gonna talk about the basics of data structures and algorithms which is one of the topics that come up in coding interviews all the time. In fact, more and more companies ask questions about data structures and algorithms to see if you can think like a programmer in this video we're going to talk about the basics of data structures on algorithms we'll be talking about Big O notation arrays and linked lists after watching this video if you want to learn more I encourage you to enroll in my ultimate data structures and algorithms course the link is below this video now to watch this video you don't need any prior knowledge of data structures on algorithms but you need to know the basics of programming in this video I'll be using Java but if you don't know Java that's perfectly fine you can code in your favorite programming language if you enjoyed this tutorial please support me by liking and sharing it with others also be sure to subscribe as I regularly upload near videos all right now let's jump in and get started
before we talk about data structures and algorithms we need to talk about the Big O notation we use the Big O notation to describe the performance of an algorithm a lot of people find Big O scary but don't worry I'm gonna make it super simple for you so let's jump in and get started so what is this Big O all about well let's start with the classic definition on Wikipedia Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity huh that's the reason why a lot of people find Big O scary but as you will see in this section the underlying concepts are actually not that hard we use Big O to describe the performance of an algorithm and this helps us determine if a given algorithm is scalable or not which basically means is this algorithm going to scale well as the input grows really large so just because your code executes
quickly on your computer doesn't mean it's gonna perform well when you give it a large data set so that's why we use the big o notation to describe the performance of an algorithm now what does this have to do with data structures well as you will learn in this course certain operations can be more or less costly depending on what data structure we use for example accessing an array element by its index is super fast but arrays have a fixed length and if you want to constantly add or remove items from them they have to get resized and this will get costly as the size of our input grows very large so if that's what we need to do then we have to use another data structure called a linked list these data structures can grow or shrink very quickly but accessing a linked list element by its index is slow so that's why you need to learn about the Big O notation first before we can talk about
various data structures also big companies like Google Microsoft and Amazon always ask you about Big O they want to know if you really understand how scalable an algorithm is and finally knowing Big L will make you a better developer or software engineer so over the next few videos we're going to look at code snippets and use the Big O notation to describe the performance of our algorithms you here's our first example this method takes an array of integers and prints the first item on the console it doesn't matter how big the array is we can have an array with 1 or 1 million items all you're doing here is printing the first item so this methyl has a single operation and takes a constant amount of time to run we don't care about the exact execution time in milliseconds because that can be different from one machine to another or even on the same
machine all we care about is that this method runs in constant time and we represented using the Big O of one this is the run time complexity of this method so in this example the size of our input doesn't matter this method will always run in constant time or Big O of 1 now what if we duplicate this line here we have two operations both these operations run in constant time so the runtime complexity of this method is Big O of 2 now when talking about the
runtime complexity we don't really care about the number of operations we just want to know how much on an algorithm slows down as the input grows larger so in this example whether we have 1 or 1 million items our method runs in constant time so we can simplify this by writing down o of 1 meaning constant time let's look at another example next you here we have a slightly more complex example have a loop so we're iterating over all the items in this array and printing each item on the console this is where the size of the input matters if you have a single item in this array we're gonna have one print operation if you have a million items obviously we're gonna have a million print operations so the cost of this algorithm grows linearly and in direct correlation to the size of the input so we resent the
runtime complexity of this method using the Big O of n where n represents the size of the input so as n grows the cost of this algorithm also grows linearly now it doesn't matter what kind of loop used to iterate over this array here we're using a traditional for loop we could also use a for each loop for example for int number in numbers we could simply print the number we're still iterating over all the items in this array we could also use a while
loop or a do-while loop now what if we have a print statement before and after our loop what do you think is the runtime complexity of this method well you saw that this single operations running constant time so here we have the Big O of one our loop runs in Big O of n and once again we have the Big O of 1 so the run time complexity of this method is o of 1 plus n plus 1 which we can simplify to or of 2 plus n however when using the Big O notation we drop
this constants because they don't really matter here's the reason if our array has 1 million inputs adding two extra operations doesn't really have a significant impact on the cost of our algorithm the cost of our algorithm still increases linearly so we can simplify this by dropping this constant what matters is that the cost of this algorithm increases linearly and in direct proportion to the size of our input if you have five items in the input we're gonna have five operations if you have a million we're gonna have a million operations now what if you had two loops here so let me delete these print statements and duplicate this loop what do you think is the runtime complexity of this method it's gonna be big-oh of n plus n or Big O of 2n this is another example where we drop the constant because all we need here is an approximation of the cost of this algorithm relative to its input
size so N or 2n still represents a linear growth now what if this method had two parameters an array of numbers and an array of names so first we iterate over the area of numbers and then we iterate over the array of names like this what do you think is the runtime complexity here well both these loops run in O of n but here's a tricky part what is n in this case we're not dealing with one input we have two inputs numbers and names so we need to distinguish between these two inputs we could use n for the size of the first input and M for the size of the second input so the runtime complexity of this method is going to be o of n plus M and once again we can simplify this to O of n because the runtime of this method increases linearly you
in the last video you learn that simple loops running linear time or Olaf n but here we have nested loops this is the algorithm that we use for printing all combinations of items in an array so what is the runtime complexity here well let's find out in our outer loop we're iterating over our input array so here we have o of n now in each iteration once again we're iterating over all the items in this array another example of oh of n so the runtime complexity of
this method is o of n times N or N squared we say this algorithm runs in quadratic time as you can see in this diagram algorithms that run in O of N squared gets slower than algorithms that run in O of n of course this depends on the size of the input if you're dealing with an array of let's say 50 items we're not gonna see any differences but as our input grows larger and larger algorithms that run in O of N squared get slower and slower now what if you
had another loop before or after this loop for example let's add a for loop and once again iterate over all the items in this array and print them on a console what is the runtime complexity of this method well here we have o of n so the runtime complexity of this method is gonna be o of n plus N squared now once again we can simplify this this square of this number is always greater than the number itself right so in this expression N squared always grows faster
than n again use the Big O notation to understand how much the cost of an algorithm increases all we need is an approximation not as an exact value so here we can drop in and conclude that this method runs in O of N squared let's look at another example what if instead of this loop we had another nested loop here so for end of third in numbers there you go the run time complexity is now o of n cubed you can imagine this algorithm gets far slower than an algorithm with o of N squared you
another growth rate we're gonna talk about is the logarithmic growth which we show with the Big O of log n here's the logarithmic curve now compare this with a linear curve as you can see the linear curve grows at the same rate but the logarithmic curve slows down at some point so an algorithm that runs in logarithmic time is more efficient and more scalable than an algorithm that runs in linear or quadratic time let's see a real example of this let's say we have an array of sorted numbers from 1 to 10 and we want to find the number 10 one way to find the 10 is to iterate over this array using a for loop going forward until we find the 10 this is called a linear search because it runs in linear time in the worst case scenario if the number we're looking for is at the end of our array we have to inspect every cell in this array to find the target number the more items we have the longer this operation is gonna take
so the run time of this algorithm increases linearly and in direct proportion with the size of our array right now we have another searching algorithm called binary search and this algorithm runs in logarithmic time it's much faster than the linear search assuming that our array is sorted we start off by looking at the middle item it's this item smaller or greater than the value we're looking for it's smaller so our target number in this case 10 must be in the right partition of this array right so we don't need to inspect any of the items in the left partition and with this we can narrow down or search by half now in the right partition again we look at the middle item is it smaller or greater than the target value it's smaller so again we ignore the items on the left and focus on the items on the right so in every step we're essentially narrowing down or search by half with this algorithm if we
have 1 million items in our array we can find the target item with a maximum of 19 comparisons we don't have to inspect every item in our array this is logarithmic time in action we have lovers meet growth in algorithms where we reduce our work by half in every step you're going to see this a lot in the second part of this series where we talk about trees and graphs unfortunately I cannot show you an example of this in code now because it's a bit too complex there are a few things we have to talk about before you are ready to see that in code but trust me you'll see that in the code in the future and it'll become super easy for now all I want you to take away is that an algorithm with logarithmic time is more scalable than one with linear time you
the last growth rate we're going to talk about in this section is the exponential growth which is the opposite of the logarithmic growth so the logarithmic curve slows down as the input size grows but exponential curve grows faster and faster obviously an algorithm that runs in exponential time is not scalable at all it will become very slow very soon again I cannot show you an example of this in code yet we'll have to look at it in the future for now all you need to understand is that the exponential growth is the opposite of the logarithmic growth and by the way these growth rates we have talked about so far are not the only growth rates but these are the ones that you see most of the time there are some variations of these that we look at in the future for now just remember these five cares you you have seen how we can use the Big O notation to describe the runtime complexity of our algorithms in an ideal world we want our algorithms to be super
fast and scalable and take minimum amount of memory but unfortunately that hardly if ever happens it's like asking for a Ferrari for ten dollars it just doesn't happen most of the time we have to do a trade-off between saving time and saving space there are times where we have more space so we can use that to optimize an algorithm and make it faster and more scalable but there are also times where we have limited space like when we build an app for a small mobile device in this situations we have to optimize for this space because scalability is not a big factor only one user is going to use our application at that moment not a million users so we need a way to talk about how much space an algorithm requires and that's where use the Big O notation again let's look at a few examples here we have this greet method that takes an array of strings and prints a high message for every name in this array now in this
method we're declaring and loop variable and this is independent of the size of the input so whether our input array has 10 or 1 million items this method will only allocate some additional memory for this loop variable so it takes all a one space now what if we declare is string array like this we call it copying and initialize it like this so the length of this array is equal to the length of our input array so if our input array has a thousand items this
array will also have a thousand items what is the space complexity of this method it's all of n the more items we have in our input array the more space our method is gonna take and this is in direct proportion to the size of our input array that's why we have oh of n here and by the way when we talk about space complexity we only look at the additional space that we should allocate relative to the size of the input we always have the input of size n so we don't count it we just analyze how much extra space we need to allocate for this algorithm so that's all about space complexity in this course we'll only focus on runtime complexity because that's a bit more tricky but from now on think about the space complexity of your algorithms especially in situations where you have limited space see if there are ways to preserve the memory hey guys Marcia I wanted to let you know that this video is actually part of my
ultimate data structures and algorithms course the complete course is 13 hours long and I've divided it into three parts so you can take and complete each part easily if you're serious about learning data structures and algorithms I highly encourage you to take this course and learn all the essential data structures and algorithms from scratch it's much easier and faster than jumping from one tutorial to another we'll be talking about various types of data structures such as linked lists stacks queues hash tables binary trees AVL trees heaps tries graphs and various types of sorting searching and string manipulation algorithms the course is packed with almost 100 interview questions these are the interview questions that get asked that companies like Google Microsoft and Amazon you can watch the course online or offline anytime anywhere as many times as you want and you would also get a
certificate of completion and a 30-day money-back guarantee it's exactly like this tutorial it just has more content if you're interested click on the link below this video to enroll in the course thank you and have a great day in this section we're going to talk about our very first data structure and one that you're probably familiar with arrays arrays are built into most programming languages and we use them to store a list of items sequentially in a section first we're going to look at various strengths and weaknesses of our race then I'm gonna show you how to use arrays in Java and finally we're gonna build an array class from scratch this is a fantastic exercise for you to get your hands dirty in the code and get prepared for more complex data structures so do not skip this section even if you know arrays well so let's jump in and get started arrays are the simplest data structures and we use them to store a list of items
like a list of strings numbers objects and literally anything these items get stored sequentially in memory for example if we allocate an array of five integers these integers get stored in memory like this let's say the address of the first item in memory is 100 as you probably know integers in Java take 4 bytes of memory so the second item would be stored at the memory location 104 the third item would be stored at the memory location 108 and so on for this very reason looking up items in an array by their index is super fast we give our array an index and it will figure out where exactly in memory it should access now what do you think is the runtime complexity of this operation it's all of one because the calculation of the memory address is very simple it doesn't involve any loops or complex logic so if you need to store a list of items and access them by their index arrays are
the optimal data structures for you now let's look at the limitations or weaknesses of arrays in Java and many other languages arrays are static which means when we allocate them we should specify their size and this size cannot change later on so we need to know ahead of time how many items we want to store in an array now what if we don't know we have to make a guess if our guess is too large will waste memory because we'll have cells that are never filled if our guess is too small our array gets filled quickly then to add another item we'll have to resize the array which means we should allocate a larger array and then copy all the items in the old array into the new array this operation can be costly can you guess the runtime complexity of this operation pause the video and think about it for a second here's the answer let's say our array has 5 items now we will add the sixth item you have to allocate a new array
and copy all these five items into that new array so the runtime complexity of this operation is o of n which means the cost of copying these items into the new array increases linearly and in direct proportion to the size of the array now let's talk about removing an eye here we have a couple of different scenarios if you want to remove the last item that's pretty easy we can quickly look it up by its index and clear the memory so here we have ol1 which is our best-case scenario but when doing Big O analysis we should think about the worst case scenario what is the worst-case scenario here this is when we want to remove an item from the beginning of the array we have to shift all the items on the right one step to the left to fill in the hole the more items we have the more this shifting operation is going to cost so for the worst-case scenario deletion is an O of n operation so because arrays have a fixed size in
situations where we don't know ahead of time how many items we want to store in them or when we need to add or remove a lot of items from them they don't perform well in those cases we use linked lists which we're going to talk about later in the course now let's see a quick demo of arrays in Java you in this video we're gonna look at arrays in Java if you know arrays well feel free to escape this video so to declare an array we'll start with the type of the array let's say we want to declare an array of integers so we type int and then we add square brackets to indicate that this is an array and not just a regular integer next we give our variable a name like numbers and here we use the new operator to allocate memory for this array here we repeat the type
of the array one more time but this time inside the square brackets we specify the size of this array let's say 3 now let's print this on the console we get this weird string what is this well this is a combination of the type of the array followed by an @ sign and then this value that is generated based on the address of this object in memory that is not useful you want to see the content of this array to do that we're gonna use the arrays class let me show you so here in sort of printing numbers you're gonna use the arrays class we type arrays as you can see in this class is declared in this package Java dot
util so we press ENTER and IntelliJ imports this class on the top now we use the dot operator and call the two string method a password array here and this method will convert it to a string and then we'll print that string on the console no let's run the program one more time there you go much much better so as we can see all items in a numeric array are initialized to zero now let me show you how to change the value of these items so after we declare our
array let's say we want to set the first item we type numbers once again we use square brackets and here we specify an index the index of the first item is 0 so here we are referring or referencing the first item we set this to let's say 10 similarly we can set the second item to 20 and the third item to 30 now let's run the program one more time so here's the content of our array beautiful now if you know the items that you're gonna store in your array ahead of time there
is a shorter and cleaner way to initialize your array instead of doing all this ceremony we can use curly braces to declare and initialize this array so here we type 10 20 and 30 and this will allocate an array of 3 items in memory and it will initialize each item according to the values we have passed here take a look we get the exact same result as before now this area objects have a field called length and this returns the size of the array let's print this on the console and see what we get so print numbers dot lengths there you go the size of this array is three and we cannot change it so if want to store four items here you have to create another array copy all the existing items and then add the fourth item this is the problem with arrays in Java so if you want to work with lists that grow or shrink automatically you'll have to use linked lists you're going to talk about
them in the next section but before we get there I'm gonna give you a fantastic exercise we'll look at that next so as you learn arrays in Java or static which means they have a fixed size and this size cannot be changed but now we're going to do something really cool I've created this array class which is like a dynamic array as we add new items to it it will automatically grow and as we remove items it will automatically shrink let me show you how that works so we create a new array object we call it numbers and then initialize it like this here we pass the initial size let's say
3 now this numbers object has a method for adding new items let's add 10 and then 20 and then 30 we also have a method for printing this array but technically this print method shouldn't be here because an array should not be concerned about printing its content on the console it shouldn't know anything about console it should only be concerned about storing these values displaying these values is a completely different concern and should not be implemented as part of this class but in this course we want to keep things simple that's why I've implemented the print method inside the array class now let's run this program and see what we get so 10 20 30 beautiful so the initial size of our array is 3 but we can easily add a new item and our array is going to automatically grow no problem we also have a method for removing items that is remove at which gets an index let's say we want to remove the last item what is
the index of this item well the index of the first item is 0 then 1 2 3 so let's remove the last item here's the result beautiful you also have one method for finding the index of an item let me show you so I'm gonna do a print statement here and call numbers dot index of this will return the index of the first occurrence of this item let's say 10 so because 10 is the first item in this array this method is gonna return 0 take
a look 0 if we pass a number that doesn't exist in the array let's say 100 it's gonna return negative 1 okay now here's your exercise I want you to build an array class that works exactly like what you saw in this video this is a fantastic exercise for you to get your hands dirty in the code especially for working with data structures and algorithms don't say all mush this is too easy I already know how to do this trust me as we talk about more complex data structures our code is gonna get more complex so I want you to treat this as a warm-up exercise so pause the video and spent 20 minutes on this exercise when you're done come back see my solution you
all right we're gonna solve this problem step by step and this is the approach I want you to follow whenever you want to solve a complex problem don't try to do too many things at once try to break down that problem into smaller easier to understand easier to solve problems so in this video we just want to focus on creating this array class and printing its content on the console you're not going to worry about adding new items or removing existing items we're gonna implement these features in the following videos so let's add a new class here we're going to right click this package and add a new Java class we call it array now in this class first we need to add a constructor so we type public array here we need a parameter to specify the initial size of the array so and length now inside this class we're gonna store our items in a regular Java array so we're going to declare a private field
private int array called items now here in the constructor we need to initialize this array based on the initial size so we sell items to new interim a of length pretty straightforward now let's implement the print method so public void print here we need to iterate over all the items in this array and print them on the console pretty easy so 4 into I we set it to 0 as long as I is less than items that length we increment I and in each iteration we simply print
items of I so in each iteration we get one item from the array and print it on the terminal now let's go back to our main class we're going to create a new array object we call it numbers and set it to a new array of 3 now let's print this object on a console so we get these three zeros but technically we shouldn't see anything because we haven't inserted any items in this array so let's go back to our array class we need another field to keep track of
the number of items in this array we cannot rely on items that length because this is the memory we are allocating initially we might allocate memory for 50 items like you might only insert two items in this array so every time we insert a new item we need to keep track of the number of items in this array how can we do that we can declare another private field private int let's call it count now back to a print method we're gonna replace items that length with count so initially count is zero and this loop is not gonna get executed in the future every time we insert a new item in this array we're gonna increment count by one so now let's run this program one more time because our array is empty we don't see anything beautiful we have completed the first step so next let's implement the insert method
alright now let's implement the insert method so public void insert we give it a parameter int item now what should be doing this method there are a couple of things we need to do if the array is full we need to resize it and also we need to add this new item the new item at the end of the current array let's not worry about the first step yet instead we're gonna do the second step which is easier so we want to add this new item at the end of this array how
can we do this well we use the items field we use square brackets now we need to pass an index what is this index this index should represent the last item in this array it's not gonna be items that length it's gonna be count so currently we don't have any items in this array so the index of the last item or the place where we should insert the new item is index 0 next time we add a new item the index is gonna be 1 and then 2 and 3 so
we said items of count 2 this new item and then we increment count by 1 or we can simplify this code get rid of this line and increment count over here so with this expression first we said items of count 2 item and then count is going to be incremented by 1 let's test our code up to this point so back to the main class we're gonna call the insert method and pass a couple of numbers 10 on 20 nanus run the program there you go beautiful so let's go back to the array
class and implement this scenario how can we tell if they are useful that's very easy we can write an expression like this if items that length equals count now in this case what should we do first we need to create a new array that is larger and let's say twice the size then we need to copy all the existing items into this new array and finally we're gonna set the items field to this new array because currently the array that the
items field is referencing is four so let's implement each step first we need to create a new array this is pretty easy we declare an int array let's call it new items and set it to a new entry of count times two so this new array is twice the size of the old array now we need to copy all the existing items here we're gonna use a for loop exactly like the for loop we have here so we need to iterate over all the existing items and reference them using their index so
four and I we set it to zero as long as I is less than count we incremented after each step now in each step we're gonna set new items of I two items I buy that is pretty straightforward finally we need to set the items field to this new array so we set items to new items now let's test this so back to the main class I'm gonna add a couple more items and run the program look now we have a dynamic array that automatically grows as we add new items to it so now that you understand how
everything works I'm gonna go back to the array class and get rid of these additional comments we don't need this comments because our code is clean and straightforward we don't need to repeat it we should only use comments for explaining wise and house not what the code is doing that should be reflected in the code itself so delete delete and delete next we're going to implement the delete operation you
alright now let's implement the remove method so public void remove at we give it an index now what should we do here first we want to validate the index and make sure it's within the right range for example if someone passes negative 1 it doesn't make sense what doesn't mean to remove the item at index negative 1 or let's say our array has 5 items as you know the index of the last item in this case showed before what if someone says remove the item at the index 5 or 6
or 7 okay it doesn't make sense so first we want to validate the index second we want to shift the items to the left to fill the hole we'll talk about what this means in a second let's implement each of these scenarios one by one so first we're going to validate the index this is pretty easy we can write an if statement like this if index is less than zero or we use two vertical bars to indicate a logical or or index is greater than or equal to count what
does this mean well if count is four that means the index of the last item is three so we cannot tell this array to remove the item at index 4 or 5 and so on so that is why we have greater than or equal to count here now what should we do in this case we don't want to print a message on the console because this class might be used in an application with a graphical user interface there we don't have a console so instead we should throw an exception because this is a programming error if someone passes an index that is out of range so by throwing an exception we forced a program to crash and with this the programmer knows that they made a mistake and they will solve this problem so we throw a new exception of type illegal argument exception that was the first step now let's work on the second part let's imagine we have an array like this 10 20 30 and 40 and then we want to
remove the item at index 1 that is this 20 over here so in order to remove 20 we should copy 30 over here and then 40 over here so we're shifting each item one step to the left in other words the item at index one should be set to what we have at index 2 and what we have at index 2 should be set to what we have at index 3 how can we implement this this is very easy we need a for loop that starts from this index and it goes all the way until it reaches the end of this
array so for int I we set this to index as long as I is less than count the increment I after each step now in each iteration we want to set the item at this index to the item to its right side that is pretty easy so we set items of I to items of I plus 1 ok so after we execute this for loop our array is going to look like this 30 is gonna be copied over here and then 40 is gonna be copied over here but we still have 4 items in this array we want to shrink this array
so it looks like this how can we do that very easy after our loop with decrement count by 1 because count represents the total number of items currently in there a not the size of the array right so let's test our code back to the main class we added four items here before printing the array let's call remove add and remove the first item so 10 is gonna go away now we have 20 30 40 beautiful let's test it with a different index let's say index 1 now 20 is gone
beautiful let's do another test and remove the last item so it pass index 3 40 is gone what if you pass in X 4 we got an exception of type illegal argument exception this is a programming mistake we don't want to print a message on the console we want to stop the execution of the program so now that we're done with the implementation of the remove method let's get rid of these unnecessary comments and make our code clean next we're going to implement the search operation
finally let's implement the search operation so public int because we want to return the index of the given item we call this method index of and give it a parameter item now what should we do here we want to loop over all the items in this array if we find the item you want to return the index otherwise we're gonna return negative one so once again we're gonna use a for loop that's pretty easy for I we set this to zero as long as I is less than count
you're gonna increment it by one now we need to get the item at the given index and compare it with this item so we write an if statement if items of I equals this item then we want to return I as the index otherwise if you finish this loop and we're still here we didn't return from this method that means we couldn't find this item so we should return negative one let's test our new method back to the main class we added these four items 10 20 30 40 let's print
numbers that index of 10 so that is 0 beautiful what about the index of a number that we don't have let's say 100 that is negative 1 so we're done with this implementation but before I remove the comment let me ask you a question what is the runtime complexity of this method pause the video and think about it for a second ok here's the answer we need to analyze the best case on the worst case scenario the best case scenario is where this item is the first
item in this array so in that case the runtime complexity is o of 1 but the worst case scenario is where this item is at the end of the array so we have to loop over the entire array to find that item if our array has 1 million items that means we're gonna have 1 million comparison operations so in the worst case scenario the runtime complexity of this method is o of as I told you before when doing Big O analysis we always consider the worst case scenario so the runtime complexity of this method is o of N you
so you learn how to build a dynamic array from scratch and that was a great exercise however Java has two implementations of dynamic arrays let me show you we have two classes vector and array list both these classes are declared in the Java that util package but they're slightly different the victor class would grow by how to person of its size every time it gets full whereas the ArrayList will only grow by 50% of its size also all the methods in the vector class are synchronized this is an advanced topic and I'm gonna cover that in my upcoming advanced Java course but basically when we say a method is synchronized that means only a single thread can access that method in other words if you have a multi-threaded application where multiple threads are gonna work with this collection that you're not gonna be able to use the victor class you should use the ArrayList class because the
methods of the ArrayList are not synchronized again I'm gonna cover that in detail in my upcoming advanced Java course now let's have a quick tour of the ArrayList class so let's type array list then this angle brackets you see here these represent a generic parameter with this generic parameter we specify the type of each element in this array list for example if you want to have an array list of integers we type integer this integer class is a wrapper around
the native or primitive int type so for every primitive time that we have like int short white boolean whatever we have a wrapper class for example we have short we have white we have boolean and so on we can also have an ArrayList of strings or students assuming that we have a student class in this demo I'm gonna create an array list of integers so integer n term now we need to import the ArrayList class because it's declared in a different package so we
press Alt + Enter there you go it's important on the top now let's create our ArrayList we call this list and initialize it using the new operator like this new ArrayList and now we can call the add method we can add a number here and duplicate this line a few times I have two more numbers now we can print this list on the console so here is the content of our array beautiful we can also remove items so remove we can remove a particular object or remove an item at a given
index for example we can remove the first item and now ten is gone we only have 20 and 30 we can also find the index of the first occurrence of an element so recall list that index of 20 because now after removing the first item 20 is going to be the first item this method will return zero we also have last index of which will return the index of the last occurrence of an item we also have contains which returns a boolean value telling us if you have
this item in our array or not and finally we can use the size method to get the number of items in this array and finally another useful method is the true array method this will convert this list to a regular array object so there are times that you want to work with a regular array object let's say you have a method that only accepts an array and you cannot pass an ArrayList class there in that case we can easily convert your ArrayList to a regular array linked lists are probably the most commonly used data structures after arrays they solve many of the problems with arrays and are also used in building more complex data structures so in this section we're gonna look at linked lists we'll talk about how they're structured in memory we'll look at the time complexity of various operations on them and finally we're gonna build a linked list from scratch again this is an incredible exercise for

Ask anything
We've read this whole study set, so questions about it get the best answers.