 Hi guys. So today we're going to be taking a look at how to implement the add method for a custom array list. Now in our previous video, we talked about growing a custom array list. And just a reminder, our customer array list contains two fields, data, which is the array that holds our items, and n items, which is the counter that keeps track of how many items we have stored in data. We then utilized the isFull and the grow methods in order to grow our array list. But today we're going to be taking a look at how we can add items to our customer array list. And the first method we're going to take a look at is just this simple add method right here, which simply adds an item to the end of the list. So you can see it's a return type void. It just takes one parameter, the item to add. Obviously, we first check to see if the array list is full. And if so, we grow our list by calling the isFull and grow methods above. We then simply add our item to the end of the list and increase our n items counter by one. So that one's fairly straightforward. But this next add method here allows us to add an item to any index that we choose within our array list. So let's take a look at how this one works. So you can see it's got return type Boolean. It takes two parameters, idx, which is the index we're going to add our item to. And item to add, which is obviously the item we're going to be adding. So first of all, we have this check here, if idx is less than zero, or if idx is greater than n items, return false. And what this is doing is it's checking to make sure that we've passed a valid index into our method. And if our index is less than zero, or greater than n items, it's not valid. So we just return false and we don't add any items to our array list. Once again, we then of course check to make sure our array list is not full. And if it is, we grow it. Then here we have this for loop for int i equals n items minus one i is greater than or equal to idx i minus minus data i plus one equals data i. So what this for loop is doing in order to make space for our new item to be added to our list items from the end of the array down to idx need to be moved to the end of the array one by one. So basically what we're doing is one by one, we're taking data from i, and we're moving it to data i plus one in order to make space for our new item. Once we've done that, we're then going to add our new item to our chosen index, and then of course increase our counter by one, and return true to indicate that our method was successful. All right, so you can see here that I've created a new my array list object called R. I've then added three items consecutively to the list 10, 70, 20, and I've then called that second add method to add the number 90 to index one and the number 30 to index three. So I've added a couple break points here so we can take a look at what's going on if we run this in the debugger. And of course, we're starting the debug mode from this line right here. So if I open up our array data, you can see we've already added the first three items 10, 70, and 20. And data is of type double. So these numbers are represented as doubles 10.0, 70.0, 20.0. And when we initialize data in my array list, it is initially of size five. So the last two indexes of our array are just filled with the default double value 0.0. And n items, of course, equals three because we currently have three items in our list. All right, so if we step through to this method here, then you can see we're going to go up to our add method here. First, we're going to make sure we have a past a valid index one is a valid index. So it'll move to check if the array is full. It isn't so we're then going to go down to this for loop here. So starting at n items minus one, which is two, we're going to loop through. And now if we open our array up, you can see that data i plus one, so data three now contains data i the value inside data i, which is 20. So you can see that our list now looks like this. Now if we step through again to i equals one, and then we step through one more time, you can see now, once again, data i plus one, so data two has taken on the value of data i, which is 70. All right, so we've now finished the for loop, we're going to add our new item to our list. And our list now looks like this, we're going to increase an items by one, because we now have four items in our list, and items now equals four. And we're going to return true. And if we now go ahead and move on to the second array list, the second add method, sorry, we'll see the same thing happen. So our array list currently looks like, sir, we're going to again perform outchecks, move down to our for loop, go ahead and run this, we can see that data i plus one or data four has now taken on the value of data i, which is 20. We've finished the for loop. So we're going to go ahead and add our new item to index three. So we've added 32 index three, we'll increase an items by one, and we'll return true. And we can see our complete array list, customer array list looks like so.