Locked lesson.
About this lesson
An array of other arrays is multi-dimensional.
Exercise files
Download this lesson’s related exercise files.
Multi-Dimensional Arrays.docx60.1 KB Multi-Dimensional Arrays - Solution.docx
59 KB
Quick reference
Multi-Dimensional Arrays
A multi-dimensional array is an array with other arrays inside of it.
When to use
Use it whenever you need to put arrays inside of other arrays!
Instructions
Arrays can hold just about anything; strings, numbers, variables, other arrays and many other things.
To creatte a multidimensional array:
my_array ["John", "Tim", "Mary", ["Elder", "Smith", "Jones"]]
To call an item inside an array that's inside an array:
puts my_array[3][1] - puts Smith
Hints & tips
- Arrays can hold all types of items!
- An array with other arrays inside is called a multi-dimensional array
- 00:04 In this video, I want to talk about multi-dimensional arrays.
- 00:07 So we already started talking about arrays in the last video.
- 00:10 And we put a bunch of strings into our array.
- 00:13 John, Bill, Mary, and Tim.
- 00:14 Now you can put all kinds of other things in your array,
- 00:17 it really doesn't matter what you put in there.
- 00:19 We can put in an integer, 41.
- 00:22 We can put 12.99.
- 00:24 So if we change this to 5, save it and run it, we get 12.99.
- 00:31 If we add -1, 11.99.
- 00:34 So this is a number, it's a float and we can do math on it.
- 00:38 We could change this to 0, and go plus 1.
- 00:43 And of course, we should get an error here because we already
- 00:46 understand we can't add a number and a string.
- 00:48 They're two different data types.
- 00:49 But anyway, get rid of all of that.
- 00:52 So we can add numbers, we can add other variables.
- 00:55 So we can call name = Susie, and we could put name in there.
- 01:01 So now 0, 1, 2, 3, 4, 5, 6,
- 01:06 if we output the sixth item, we get Susie.
- 01:11 So strings, numbers, floats, integers, other variables.
- 01:15 Now what I really want to talk about in this video multi-dimensional arrays.
- 01:19 We can put strings, we can put numbers, we can put other variables, and
- 01:23 we can put other arrays.
- 01:26 This is really going to blow your mind.
- 01:27 So 1, 2, 3, Josh,
- 01:32 Tina, whatever.
- 01:36 So now, if we save this thing and
- 01:38 let's just print out our whole array and see what we've got here.
- 01:43 We see we have John, Bill, Mary, Tim 41, 12.99 and Susie.
- 01:48 Then we've also got this 1, 2, 3, Josh and Tina.
- 01:52 So what's going on here let's clear the screen.
- 01:54 Now let's call, let's see we've got 1, 2, 3, 4, 5, 6, 7, 8.
- 02:02 This whole thing is the eighth item in our array, or in this case number seven,
- 02:07 item number seven.
- 02:08 So if we save this and run it, we get 1, 2, 3, Josh and Tina.
- 02:12 So how do we access something in our array of arrays here?
- 02:17 Well, we want something from item number seven which is this array and
- 02:22 then we want the, let's say zeroth item from that.
- 02:26 So if we save this and run it, so the zeroth item is 1, 0, 1, 2, 3.
- 02:32 Let's see what the third item is our brackets disappeared.
- 02:36 So we save this and run it.
- 02:38 We get Josh.
- 02:38 So this is a multi-dimensional array.
- 02:42 It's an array with other arrays in it.
- 02:44 And like I said, you can put all kinds of stuff in arrays including other arrays,
- 02:48 including other objects, methods, all kinds of things.
- 02:51 Hashes we can do I believe, we'll get into that much later on.
- 02:54 But those are arrays, those are multidimensional arrays.
- 02:57 The first time round,
- 02:58 it's kind of tricky to think about an array inside of an array.
- 03:01 You gotta kind of work it through in your brain a little bit say, okay,
- 03:04 that's how it works.
- 03:05 But pretty easy, just name the item, this is the seventh item.
- 03:09 This whole array is the seventh item.
- 03:11 And inside of that we just call like we would a regular array.
- 03:14 So inside of this array we could do another array, and
- 03:17 we just keep adding onto it if we wanted to.
- 03:21 So those are arrays.
- 03:23 In the next video, we're going to start talking about loops,
- 03:26 specifically the while loop.
Lesson notes are only available for subscribers.