 Last time, we looked at how we could store a one-dimensional array into memory, and this worked out really well because our array is one-dimensional and our memory is one-dimensional. But we'd also like to be able to store data structures that have multiple dimensions. So this time, we're going to start by looking at how we can store a two-dimensional array into our one-dimensional memory, and then we will see how we can extend those methods to store arrays of arbitrary dimensionality. We're going to look at two different ways of storing a two-dimensional array into our one-dimensional memory. The first is the traditional method. Basically, we're just going to build one big long array that has enough space for all of our elements. So here I've got 15 elements in this table, so I will generate an array of 15 elements in it. Now, what I'm going to do is I will start at the left-hand side, and I'm just going to start tossing elements in here, so I've got 0, 0, 0, 1, 0, 2. You can see I'm just walking across the top row there. But once I get to the end of the first row, then I'm going to go on to the second row. I'll start with 1, 0, and I'll work my way across the second row. Once I get to 1, 4, then I'll start over again with 2, 0, and work my way across the third row. So in this case, I'm just taking rows out and tagging them on at the end after the previous row. And I can just keep doing that until I've converted my entire two-dimensional array into a one-dimensional one. This was the first method we came up with for storing multi-dimensional arrays, but there's a second one. We could use pointers. This time I have some core array that just consists of pointers. Each one of the elements in my base array here is pointing to some array that actually contains data. So now I've got three arrays, each of which contain the data from one row. And then I've got another high-level array that just tells me where that data is. So essentially I have an array of pointers to all the rows, and then each of these row arrays has the data from each column. So I can take these methods and I can extend them to multi-dimensional arrays, basically just by applying my method and doing it multiple times. If I was going to use the traditional method for, say, a three-dimensional array, then I'd start with, say, the first plane. I'd walk over each of the rows in that plane. Then I would go on to the next plane. Walk over each of the rows in that plane. Then go on to the next plane. So I'd still be doing the same thing I did before, but now I've got an extra dimension. So I have this extra loop that I'm iterating over. If I'm going to use the pointer method, then I'm going to do something similar to that again, where I've got an array of rows which is pointing to all of the data, and now I'll have another array that would be pointing to all of the planes that I've got. So that array of pointers, where they were all pointing to multiple rows with data, is enough to implement one plane. If I want to have multiple planes, then I would have an array of planes where each element is pointing to one of those arrays of rows. Then I could extend that to four dimensions or five dimensions just by repeating that process. We're going to be doing a lot of the same thing, and we pretty much just need to know how many dimensions we've got. So why would we want one of these over the other? The traditional method is nice because it's fast and it's small. We're only allocating as much space as we need for our data, and our compiler can figure out exactly where that data is. As long as we know where our base address for our object is, we can reference any other cell in there directly. In contrast with the pointers, well, we have to allocate extra space for the pointer arrays, where each array is pointing to another array. Then eventually we end up with a set of arrays containing our data. So we're allocating a little bit of extra space, and because we're going to have to make a lot of memory calls to find where our data is, it's going to be slower. But the pointer method means that I don't have to find contiguous space for my arrays. If I have a really large array, it may be hard to find enough contiguous space to hold that entire array. With the traditional method, I have to be able to store all of the data into one large block. But if I've got a really, really large array, multiple gigabytes, multiple terabytes, I may not have that much space in memory. My operating system may be able to move some stuff around to make that space for me, but it may not, and it may not be willing to. So both of these methods have some advantages and drawbacks, but we can use either one of these to implement a multi-dimensional array.