Check out a free preview of the full The Last Algorithms Course You'll Need course:
The "Arrays Q&A" Lesson is part of the full, The Last Algorithms Course You'll Need course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen answers student questions about whether there is no insert, push, or pop in an array and if an array's size and memory allocation must be specified at initialization. Questions regarding whether something that has an array is being created when creating an array in JavaScript and how big the array is that is instantiated are also covered in this segment.

Get Free Access Now

Transcript from the "Arrays Q&A" Lesson

>> Any questions on what we've covered up to this point on what arrays are.
>> So you're saying there is no insert or push or pop in arrays.
>> The question was, there is no insert or push or pop and an arrays? Technically no, not if it's a static array or a traditional array, there is no methods on it In fact, in some of the older languages, there's not even dot length, right?

[00:00:21] You have to pass the length as well as the pointer to the beginning of the array and just say, hey, here's my type. Here's where it starts. Here's how many I have. That's why when you look at any C program, the entry point is an array of strings.

[00:00:37] And how many strings they are? Right, like it it has to tell you how to interpret these pieces of data.
>> If an array doesn't grow, does its memory and size and memory allocation have to be specified at initialization of the array?
>> Whenever if ,so if you use rust and you try to use an array, you will realize right away that you have to put in a size.

[00:00:57] It has to be a specified size and often you'll see this all over the place that you have to like tell it its size and that's the size that it's created. Now, it can be a variable still, right? You can still create an array with n as its size in a lot of these languages but it still has to be a specified size.

[00:01:13] There are also like compiled time arrays where you actually have to be like this thing is a 3 it's always a 3, it is always going to be a 3, right? There's there's kind of these different styles. But nonetheless, an array has to have its size as part of its allocation.

[00:01:27] You can't grow it, you can reallocate it, meaning you're going to have to create a new array. Take your old array, write it into the new one, you now have a bigger array. You now have more room but nonetheless, it is these fixed sizes. So, the question was, when you're creating an array in JavaScript, are you creating something that has an array underneath the hood?

[00:01:47] In some sense? Yes, you are definitely create there is an array. There is a literal array at some point ,there is a memory buffer at some point underneath the hood, but we don't see it that way in JavaScript. And you also don't see it in Java. Java has this exact same data structure that they use obviously JavaScript.

[00:02:02] Again, there's a bunch of conditions that are a part of your array that make it actually a lot more tricky to simply to. I'm sure they have a turn it into a map once you have too many items or once you have too sparse of an array, I'm sure they have a lot of algorithms that make it 10 times more complicated than this.

[00:02:18] But for the most part it is. It operates like an array. I don't wanna give it away. I don't. 1 more question.
>> I guess how big is the array then that you instantiate? So, that is part of optimizing. So, like in Rust, if I'm not mistaken, if you create a new vector, it's gonna create a effectively a memory buffer with the size 5 underneath.

[00:02:42] And so, as you push and pop, it has 5 units for you to be able to play and now is that efficient? Well, that's the problem. How big do you need to create your underlying buffer to be able to effectively use that space without having to re allocate too much or without not using it?

[00:02:58] Right, cuz we could all create 10,000 units and be like, hey, look, we'll never have to reallocate again. But if 10,000 units are big units, you're gonna use a lot of memory, just to have that in there. So, it's kind of it's a game people play and we'll go over these it'll make a lot more sense once we go over the data structures that are used.