Check out a free preview of the full Complete Intro to Computer Science course
The "ArrayList Practice" Lesson is part of the full, Complete Intro to Computer Science course featured in this preview video. Here's what you'd learn in this lesson:
Brian provides a short exercise to practice implementing an object with ArrayList and then live codes the solution. Student questions regarding what to do if the index is not found, could the delete method be reused for pop, and if the ArrayList should have a peak function.
Transcript from the "ArrayList Practice" Lesson
[00:00:00]
>> Let's go ahead and go implement something with ArrayList. So I have the arrays up here we're going to get ArrayList.test. And I have a couple of things that I want you to do here. So I want you to implement an object. You have this class here, ArrayList.
[00:00:23]
And I want you to have an integer for the length, right? So you're gonna have to keep track of what the length of your particular object is. I want you to implement a push and a pop method, so this is basically gonna be a stack at the end of the day, right?
[00:00:40]
So you can push numbers on the end, right? You can pop numbers off the end. And then I want you to implement a get function and then a delete function. Which accepts an index, removes the value from the list and then it will do a collapse on that particular part, right?
[00:00:58]
So if I delete from index 1 of my length 10 array, you're gonna have to collapse all those various different methods. And so if you've never worked with classes before, let's just make sure that you get a clear not testing on classes. So you're gonna have a constructor, probably.
[00:01:16]
This is where every time that I call new right here, Right, where I say new ArrayList right here, the constructor method gets run. So here you're going to instantiate all your variables. You're gonna have a push method, which is going to accept a value. And this is going to add an item to the end of the array.
[00:01:50]
A pop method which doesn't have anything. This is going to remove the last item in the array. Okay, and returns it. You're gonna have a get method, Which is going to take an index, And returns that item from the array, And you're gonna have a delete method. Which takes an index removes item from the array and collapses, The array.
[00:02:44]
When I say collapse, right? You need to shift everything over. So you can call this collapse. You can call it shift. You can do whatever you want there, okay? You are not allowed to use arrays at all for this. So everything needs to be done in terms of just saying I mean, let me just look at how I did it here.
[00:03:16]
Yeah, so I have something here that I just said this .data equals empty object, right? And then I use the data object to store all these various different values in. In fact, I'll just do this for you. This is the constructor that I have. So go ahead and just copy that.
[00:03:40]
Cool, any questions about the exercise here?
>> So if it's not found the index for the good function what it should return? Avoid zero is good, undefined. I don't think I test you on it. So I don't think it matters. But in that particular case, I would. You can either throw an array out of bounds or you could return undefined So, push for an ArrayList is pretty straightforward.
[00:04:16]
This.data, this.length, Equals value, this .length plus plus. This is the advantage of ArrayList. This is a very, very straightforward operation. Pop pretty similar so cons response whatever you wanna call this, equals this .data, this .length-1. You probably wanna have some sort of logic to make sure that you're not popping off and array link 0.
[00:05:01]
In this particular case, I'm not really worried about corner cases. I just wanted to kind of get the gist here. I'm gonna say delete this .data, this .length -1. This .length, minus minus return response. Okay, that'll remove something off the end of there. The get it's the most beautiful part of this is return.
[00:05:38]
This.data index. Right, the whole reason for ArrayList is that this is very fast and very simple, right? As you can imagine, there's basically no bugs that that could happen here, right? I guess you'd have to worry about array or index out of bounds, but that would be just about it.
[00:06:04]
Okay, and then delete. This is where things get a little hairy. We're gonna say const response = this.data[index]. And then we'll return this at the end. You could also could just return true or something like that. That's totally up to you. But here we're gonna have to do something to delete this.
[00:06:34]
So I'm gonna write a method called collapsed two. So I'm gonna say this, this collapsed to index, if you've never seen the underscore leading, this is kind of how you symbolize in JavaScript a private method. Since JavaScript until very recently didn't have private methods. Technically, you can put up a pound here, And then you could say collapse to Yeah, so this actually is not enabled right now in the in code sandbox and we're not gonna to go enable it.
[00:07:19]
But that's how you would actually do a real private method in JavaScript. Let's just replace that with an underscore. Honestly, I like this better anyway, it allows me to mess with the function which is kind of fun, right? It's fun to mess with the react internals and stuff like that.
[00:07:35]
But apparently they don't like it. [LAUGH] All right, so we're gonna have this method called collapse to what it takes in an index. And we're gonna say for, let i=0. Sorry, let i=index rather i is less than the length i ++. Okay, and then we're gonna say this .data i = this .data i+1, Then we're gonna to eventually say delete this .data, this .length -1 cuz we're eventually gonna move that to kinda our we're removing the last one from the array and then we're gonna say this.length--.
[00:08:38]
So this is starting i at the index and then from there until the end, we're going to shift everything over to the left one. That's what this part does, this for loop, we're going to delete the one at the end, right? And then we're going to detriment the length.
[00:08:57]
Okay, and that should be, Enough, I think for us to pass the unit tests here. So if we go down to arrays. And I think I need to remove the skip, I do. Let's see all messed up, this.data is not a function. Yep, sorry, that should be square brackets, not, Parentheses.
[00:09:30]
Let's try that again. And there we go, passing the tests. I hope you gained a healthy appreciation of what arrays do for you. [LAUGH] If nothing else, that's one good takeaway from this. The question is, could we technically we use the delete method for pop? Yeah, I think that would absolutely totally work.
[00:09:52]
It's probably even as you just say this. Well, I guess you'd have to, yeah, return this start delete this .index -1.
>> That's going to be this dot length minus. Because of the length to be the index rate, this .length .index
>> Yeah sorry, not, yeah, length. And normally hold it does pass.
[00:10:32]
Yep, so that's probably even better to just reuse the logic that you have.
>> Shouldn't it also have a big function as well, or it's four different data structures?
>> She does have a what as well?
>> A peak.
>> Yeah, I mean, if we were implementing a truth stack, you would have a peak as well, which would be basically this .length- 1, right?
[00:10:57]
But we're not implementing a true stack. Did I say peak up here? Yeah, so, yeah, to be a real stack, you would have peak as well.
>> Good, thanks.
>> Yep. Cool other questions? So takeaway here get really great with the ArrayList. Delete kind of annoying. So if you have something that you're going to be reading from and unnecessarily adding and removing a lot of things from ArrayList, typically your best bet.
[00:11:37]
Now, if we have something that's going to be Written to and deleted from a whole lot but not necessarily read from as much. There's another thing we can use called LinkeLists and that's what we're going to do next.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops