Locked lesson.
About this lesson
What are arrays and how do we create and use them?
Exercise files
Download this lesson’s related exercise files.
Arrays.docx58.7 KB Arrays - Solution.docx
58.9 KB
Quick reference
Arrays
Arrays are containers that hold lots of things.
When to use
Use them whenever you have lists of things to store.
Instructions
To create an array:
my_array = ["John", "Tim", "Lisa"]
Items in an array are numbered, and the first item is number zero.
To call a specific item from your array, reference it's number:
puts my_array[1] - puts out Tim to the screen.
Hints & tips
- Arrays are containers that hold many items at the same time
- Items in arrays are numbered, the first item is number 0
- 00:04 In this video I want to talk about arrays and
- 00:07 arrays are another very fundamental programming concept.
- 00:10 All computer programming languages have arrays, they're very useful,
- 00:14 you're going to use them forever.
- 00:15 So an array is, think of a variable, we said a variable is like a bucket,
- 00:20 you can put things in it, it's like a box.
- 00:22 Well, you generally put one thing at a time in a variable.
- 00:25 In an array it's the same thing, it's a big bucket, but
- 00:28 you can put lots of different things at the same time, as many things as you want.
- 00:32 And then you can pull out each one separately, you can pull them all out,
- 00:35 you could do all kinds of things with them.
- 00:36 So, to create an array, it's just like creating a variable.
- 00:39 We name it, so let's call this first_names.
- 00:43 And then use your assignment operator.
- 00:45 And to create an array we just use these straight brackets.
- 00:49 Now, to put things in there we just type whatever we want, separated by commas.
- 00:56 So we could have John, we could have Bill, we could have Mary,
- 01:02 we could have Tim, whatever you want, as many as you want.
- 01:07 And there you have your array.
- 01:09 So if we put first_names, let's see what happens here if we run this.
- 01:15 We get our nice little list, John, Bill, Mary, and Tim.
- 01:19 So that's actually pretty cool.
- 01:21 Now normally you're not going to want to just print out a list of things in your
- 01:24 array, you're going to want to pull a specific thing out of your array.
- 01:28 And arrays in Ruby, these are numbered indexes so
- 01:32 each of these items corresponds with a number.
- 01:35 And the first item in your array is always zero.
- 01:38 So John is the zeroth thing in our array, it's the zeroth item.
- 01:43 Bill is the first item, Mary is the second, Tim is the third,
- 01:46 and that's confusing for a lot of people.
- 01:48 Even to this day, every once in a while I forget, and
- 01:51 I want the first thing out of here so I type in a 1 and I get Bill.
- 01:55 And then I go, yeah, I forgot, arrays start with zero.
- 01:57 So just burn that into your brain, arrays start with zero.
- 02:00 So to call a specific item out of your array, we stick with this theme of these
- 02:05 straight brackets here and you just type in whatever item you want.
- 02:09 So first_name[0], that should output John.
- 02:12 So we run it, John.
- 02:13 So let's say we want Tim.
- 02:16 Tim is the zero, one, two, third item, so we save this and run it, Tim.
- 02:22 If we asked for the fourth item, we get nothing because there aren't four items.
- 02:27 So you might want the last thing in your array list and
- 02:30 you might not know what that is.
- 02:31 Remember in the last video, we did that length?
- 02:35 You can use that here too.
- 02:36 So if we save this, we see there are four items in our array.
- 02:41 So one, two, three, four, they're named zero, one, two,
- 02:46 three, a little bit confusing there.
- 02:47 So let's say we want the last item,
- 02:51 we can call [first _name.length].
- 02:56 And we know that will return four so we have to do- 1.
- 03:00 Getting a little complicated here.
- 03:02 Now if we run this, misspelled names, see, errors, always errors, so we get Tim.
- 03:08 So what this is doing, it's calling the length and remember our length is four.
- 03:12 But since it starts with zero, we need to subtract one.
- 03:16 So zero, one, two, three, is 4 -1 = 3, so we get Tim.
- 03:23 So just an interesting little thing.
- 03:25 Those are arrays, very, very useful, you'll just use them forever.
- 03:29 Arrays are just one of the most common programming type things.
- 03:33 So in the next video will look at multidimensional arrays.
Lesson notes are only available for subscribers.