This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 6: Array List" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- In this exercise you will approximate an implementation of ArrayList. Brian begins the exercise by stubbing out the class and member functions needed in the implementation. He then gives the group time to complete the exercise.

Get Unlimited Access Now

Transcript from the "Exercise 6: Array List" Lesson

>> [MUSIC]

>> Brian Holt: What we're gonna do now is I'm gonna work the very first part of this with you, so you can kind of get started. But I want you to actually implement this collapsing part of it. That's the part I'm most interested in having you, because I want you to feel the pain of collapsing an array.

>> Brian Holt: It is painful. [LAUGH]
>> Speaker 2: That's what we all wanna hear right now.
>> Brian Holt: You know it, hey, you're the one who decided to come to my class, so.
>> Brian Holt: Okay, so I kind of gave here a description of what I want you do. I want you to have a length, I want you to have push, I want you to have pop, and I want you to have get and delete.

[00:00:46] And then probably internally, you're gonna use some sort of collapse to method. But I'll leave it up to you to call it whatever you want. So we're gonna use ES6 classes. Some of you might find those kinda gross, a lot people do. But they lend themselves well to this problem, so we're just gonna go ahead and do that.

[00:01:03] This is ES6, and you're just creating a new class, right? Nothing too,
>> Brian Holt: Interesting there. We're gonna have a constructor. And I gotta remember all the methods that I put on this.
>> Brian Holt: Okay, and I'll do the constructor for you since this will help you what you're supposed to do.

[00:01:32] this.length = 0 and = object. So I'm gonna make you use an object instead of
>> Brian Holt: An array, because if you use an array that's, cheating, right? Because we're implementing arrays. But basically, I want you to make it so array.0, right, or 0 = 'thing', right? I want that to still work.

[00:02:02] Actually not really, it's gonna be array.
>> Brian Holt: What do I do here, I just have you do push, right?
>> Brian Holt: But yeah, you're gonna have to use an object instead of an array. That's what I wanna get at, and then you're gonna have push.
>> Brian Holt: Push(value).
>> Brian Holt: You're gonna have pop, which is gonna pop something off the top.

[00:02:28] get(index).
>> Brian Holt: delete(index).
>> Brian Holt: Something to note here, for those of you unfamiliar with the class syntax, there's no commas. I don't know why they decided not to put commas in classes, but there are no commas in classes.
>> Brian Holt: Okay, I called this _collapseTo.
>> Brian Holt: This is just an interior utility method, that I would suggest that you implement, that basically says, I'm going to delete index four.

[00:03:06] So I'd pass four into here, so I need you to shift everything else back up one, right, does that makes sense? You can do that inside a delete if you want to, I just wouldn't suggest it. And then you can ignore serialized cuz you won't need it.
>> Brian Holt: Okay, so surprise, surprise.

[00:03:28] I did your first unit test for you, which I think just checks if it's actually a thing, so good job. Yep, so it exists. Now I want you to go in and fill in all of these methods.