what's up guys this is teddy welcome to my youtube channel this video we're going to be finishing up a raise with array deletions part two of the video and in the first video we covered insertion so if you're looking for insertions check out part one and if you're looking just for an overview of arrays in general once again check out part one so on onward we are going to be deleting from the end of an array and deleting from the end of array really isn't that difficult so we go in here we have like an
eight in here and literally all that's gonna happen is we're just gonna blow off this part of the array like this part of the array is going to disappear so to speak it's not going to disappear from memory we're just going to we're just going to make it so that the length is trimmed down so that whenever we iterate over it into a like an algorithm it will not actually be it will still be there but it'll still show up and that's kind of what we want when we're pragmatically you know coding a lot of
these algorithms you don't really want you can't delete like the end of the array you know the array is fixed so what you want to do like i said you want to just literally um i guess like imaginatively or programmatically delete the end of the array because you can't it's fixed you can't really go back in you know time and delete it or you would have to go back in time to delete it so we just we shortened the length and if that's confusing you know don't worry because i'm going to explain it here in
a second so what we want to do we want to go in here and we're going to go into array we're just going to declare like a good old array again and then we're going to let's do 9 we're going to declare 9 in here and then as always we need to have that length and we need to have that length because if you iterate over this length the length that we have with actually built into the inter array like here you know you probably know what i'm talking about if you're taking any beginner program
courses you have a length that is not really that's just the capacity it's not actually tracking anything and we can't really manipulate that value so that's the reason that we need to actually declare a length now we need to go into this and we need to actually populate it with some values like if we just don't have any values it doesn't really even make sense either and we're going to give the way that we're going to do this is we're just going to make a good old for loop and we're going to give it six
values i'm having trouble here all right plus plus go down here declare that and we're just going to go into our enter array and for each part we're going to go into the length and we're going to actually add and then after each running of this for loop we're going to decrement it so if we don't do if we just didn't decrement it it would it would just sit there and replace like the same value over and over so we need to make sure that we're actually incrementing that length otherwise it's not going to work
then we're going to go to here we're going to go down here and this is going to be the easiest one that you'll probably ever ever see deleting from the end of an array is like super easy but it can kind of trip you up um let me show you what i mean so here i'm actually going to go and go in here i'm actually gonna show you guys so we've got our length and we need to um make sure that this part is the length right here do not do int array dot length otherwise
it's not going to work and this kind of goes back to what i'm saying before you can't since you cannot actually delete like the since an array is fixed you can't actually delete the end of it i'm going to go here and go into this console.writeline and we're gonna go ahead we're gonna spit out all the elements in that array and show you guys what i'm talking about and if you're coming in from another emma if you're coming in from another video what i like to do is i like to go down here and i
like to add watches so we're going to go ahead we're going to run this and i've already got these watches down here and what it does is it creates like a little window for your variables that you only want to watch and what i do is i go add watch i'm going to go down here and you can see what's happening so compare these two let's just let's just run through it and i'm going to show you something very peculiar it's going to be really cool it's going to be very representative of what we're trying
to do here so we're going to go down here and we've got our our ray is full but we didn't actually delete anything like that you'd be like well teddy like i'm looking in my watch right here i'm not actually seeing anything being deleted but then i iterate through this length length and then you will see the actual value or you'll see i'll show you you'll see the actual values all right i'm just actually going to delete this and then i'm just going to run it so when you actually run it you actually do get
one that's being popped or one that's being logically taken off it's being logically it's not it's still existing memory because the array is fixed but it's logically taken off so if we were to actually use this algorithm it's literally like we deleted one from the end of it and that is in a nutshell what we want it's really easy all you have to do is just take you just have to decrement the length if you're trying to actually delete something from the end of the array and that's going to be the most simple array deletion
that you're probably ever going to have to have now we're going to delete from the start we're going to stop drawing so deleting from the start so i'm going to get out my handy pin here and i'm going to kind of just logically show you guys how this happens so we're going to delete from the start that's blown away and then we're going to go in here we're going to go actually move all these over and i'm just going to go here delete this one and i'm going to change this to red so you guys
can see it and what's going to happen is this one's going to go over here this one's going to go over here this one's going to go over here this one's going to go over here over here over here and i want to wipe out all these numbers because they're going to be totally different go down here wipe all these out and what's going to happen is is our array is going to look like this it's going to be 2 three four five six seven and everything's moved over and we've actually got a real deletion
all right so i'm gonna go back into visual studio then i'm going to go down here and actually do the algorithm for this for this to actually happen so remember delete that length if you want to follow along you could you could you could leave it if you wanted to but i suggest you leave it uh follow along and we want to have that one there otherwise it's going to um not move over the first one so we're going to go in here make sure make sure that one's there otherwise not going to work or
you'll get an out of bounds exception and we're going to go in here then we're going to go into ray we're going to go i and then when we want to move something to the left we use a negative one as opposed to a positive one then we're gonna go here we're gonna go into array we're gonna go i and that should do it and it had that red squiggle but the red squiggles gone and of course you always want to logically remove you always want to take off that last one you always want to
decrement the length after you get done doing any of these operations so we're going to go ahead we're going to decrement that length and let's watch what happens so we're going to go into our debugger we've got our watches set up it's going to go ahead and declare it let's go ahead bust open that little array right there see what's in it we're going to go in we're going to add add some values okay and then watch what happens i'm gonna go ahead we're gonna move all that over then we're gonna take off that length
go ahead iterate through all this so boom boom boom boom boom okay it's uh the debugger with the console is kind of finicky so i'm just gonna take off this debugger and we're just gonna run it run it without the debugger and you see we've got one two three four five six seven eight it's correctly taken off the zero at the beginning of it and we've got our perfect little uh array right here we've got all of our values and we've got a nice logical array so we've done that now we're going to move on
to the very last one and we can actually leave all of this down here and i'm just going to bring this up here because we're kind of got a lot of stuff going on and this is going to be what actually prints it okay so then the last one is deleting anywhere from an array so let's just say we are going to delete number five and we're going to draw like a green circle around to denote which one that we want to actually delete so when we delete number five what we're going to do we're
going to blow it away then we're going to go in here we're going to move all these over so we're going to go boom boom then after we get done with that what it's going to look like is not going to have that anymore not going to have that anymore then what we're going to have is we're going to have a 6 a6 that looks terrible so i'm going to redo that and i will i'm about to invest in a wacom tablet here in a second so bear with me here so we're going to have
a six we're gonna have a seven that one's gonna be empty and it's going to be blown away so let's go ahead get rid of that and i'm going to show you guys what to do so we're going to go in here and as always we've got our int we've got i got is equal to 2 then we're going to go into our length we've got i plus plus then go down here we're going to go in ray is equal to i minus 1 and then we're going to move all these over so this is
going to be like what moves over all of the arrays i'm going to go into array again we go down here and remember whenever you see something like this that means it's moving it over to the left so we're moving that over to the left and then all we're gonna do is we're gonna go i minus and if you notice it's literally no different from the beginning it's just that we're actually starting from the length and we're we're gonna delete we're gonna it's actually starting with the length as opposed to uh the other one where
we're starting at a specific area so this one we're gonna start at two and you can wherever you want to change it and wherever you whatever number you want to blow away is going to be or delete is where you're going to manipulate that so we're gonna go say we're gonna we want this to be number four so we're gonna go in and watch what's gonna happen it's gonna do the exact same thing it's gonna go it's gonna declare all these beautiful little values it's going to fill up full of beautiful values look at that
then it's going to go down here and as you can see let me show you so we got a zero and for some reason debugger is not working good with the console log so i'm just gonna get rid of it i'm gonna show you exactly what pops up and right here we have zero one two three four five and if we want to change where the actual value is that we want to um actually delete that's what you do right there so if you wanted to do three you could do three you could do three
right here you can delete values based on wherever that you want to you just have to manipulate this this three right here and that's kind of like an important concept to remember because it's gonna help you a lot in the future be in uh allow you to be able to you know see which one that you want to delete anyway that's the video on array deletions that was a harrowing experience i hope that you guys enjoyed this as much as i did if you liked this video make sure to hit that like button make sure
to hit that subscribe button and as always thank you for watching