02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Setting a size to our Array

Creating an array with a specified number of elements.

Video Transcript

00:00What I want to do next is what I want to do is I actually want to see how could we actually define as we said
00:05How could we define a size to our array now the easiest way to define a size to our array is just in our
00:11Constructor to give it a property and the only property we have to give it is the length of our array
00:17So I'm going to tell my array that its length will be 10
00:21Now if I save my main again, and I run the application one more time
00:24It's going to tell me now that there are 10 positions inside of my array
00:28Now we haven't seen yet. How could we approach our array and how could we read the data that's in it?
00:33So let's see how could we read the data in our array?
00:36So I'm going to add here another little trace
00:38And I'm going to type a because that's the name of our array that we've created
00:42I'm going to open a bracket same as we did with the associative array, but this time around instead of sending strings
00:47I'm going to send a digit a number and that number would have to be anything between our array
00:54And now to answer another question
00:56Which is a leading question the first position of an array is not point position one
01:01But position zero thus if we created an array that has 10 positions
01:05We actually have access to 0 through 9 because 0 plus 9 we got 10 there and the last digit is the length
01:13So if we wanted to take the last element
01:15We would take our a dot length minus 1 and we would get to the last element which in our scenario would be 10
01:22So the last element would be 9 though. Okay, so let's just run this little sample here for one second
01:29And we're going to see that our value is undefined. Okay, because there's no data inside of it
01:35Our array is created, but there's no data inside of position number four
01:40Let's see what would happen if we would try to approach a position that doesn't exist at all
01:43If we run our application again, and we try to run it in a position that doesn't exist flash is not generating an error
01:49Okay, it doesn't generate an error
01:52But let's see what would happen if we'd actually try to put inside something into a position that doesn't even exist
01:59So if I put into position 20 some sort of data, let's put here the word window. I
02:05Know very random. I'm just going to save it and let's run the application
02:10So first of all, we'll see that as soon as we added a value into our a look what happened to the size of our
02:15array
02:17Because we manually position something in position number 20 flash was smart enough
02:22But at the same time that is a mistake that we might encounter which we wouldn't really want to do
02:27So even though we set our array to be a size 10 because we position something in position 20 our array now added
02:3520 10 more positions or miraculously 11 more positions
02:40Because as we said we started from 0 so from 0 to 20 is 21 items
02:43So now I've created 10 more items in our list, which I did not mean to create
02:48Okay, so this is a very very important thing to know
02:51We want to make sure that we don't do that and because we don't want to do that
02:55there's a lot of properties and ways to actually work with arrays to
02:59Avoid working in this way and I would say that usually I never really work in this way
03:04This is probably saying that is much more standard in other programming languages where they would actually generate an error if you would
03:11Put or try to put something that doesn't exist
03:14ActionScript on the other hand is smart enough and flexible enough that it grows automatically and because of that
03:19It's much easier to work with the built-in methods that enable us to add things to our arrays removing things from arrays

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!