Jonathan Kindred

Greenhorn

Posts: 2

posted 12 years ago

In K&B's book, there's a test question (#11, first self test) that has a couple of loops that create and populate a multidimentional array. The problem is--I don't understand what the "length" is of a multidimensional array.

Second, I've compiled and run the question and can't match their answer. Can anyone recommend a really good source on multidimensional arrays? Every book I have stops at grids.

Second, I've compiled and run the question and can't match their answer. Can anyone recommend a really good source on multidimensional arrays? Every book I have stops at grids.

posted 12 years ago

Allow me a bit of long-windedness in answering your question. I'm long-winded anyway

Think of a multi-dimensional array as a bunch of boxes of different sizes. Here's a 3-D array example:

Let's put 12 big boxes in a line on the floor.

In the first box, let's put 9 medium boxes.

In the second box, let's put 2 medium boxes.

In the third box, let's put 16 medium boxes.

Let's place nothing in any of the other big boxes.

In the first medium box in the first bog box, let's put 128 small boxes.

In the second medium box in the third big box, let's put 14 small boxes.

Let's place nothing in any of the other medium boxes.

Now, let's place figurines of circus animals in the first 76 small boxes in the first medium box of the first big box.

Finally, place figurines of circus animals in all 14 small boxes in the second medium box in the third big box.

Quite a mental image, isn't it? Let's simplify a little bit by translating to Java Code:

What did we lean about multi-dimensional arrays from this? Take a look at lines 12-14. (In Java) A 3-dimensional array is nothing more than a 1-dimensional array that contains 2-dimensional arrays. So, in this example, the length of our 3-Dimensional array is 12, becuase it is a 1-Dimensional array that can contain 12 2-dimensional arrays. Likewise, the length of the 2-dimensional array that is the first sub-array (a3DArray[0]) is 9, because is is a 1-dimensional array that can contain 9 1-dimensional arrays.

Finally, note that a3DArray[10] is null. We didn't put any medium boxes in there, so there's nothing there. Also note that we can't put any figurines or small boxes in the big box; the compiler prevents us. Only medium boxes (2-D arrays) can go in the big box.

So, in Summary:

Think of a multi-dimensional array as a bunch of boxes of different sizes. Here's a 3-D array example:

Let's put 12 big boxes in a line on the floor.

In the first box, let's put 9 medium boxes.

In the second box, let's put 2 medium boxes.

In the third box, let's put 16 medium boxes.

Let's place nothing in any of the other big boxes.

In the first medium box in the first bog box, let's put 128 small boxes.

In the second medium box in the third big box, let's put 14 small boxes.

Let's place nothing in any of the other medium boxes.

Now, let's place figurines of circus animals in the first 76 small boxes in the first medium box of the first big box.

Finally, place figurines of circus animals in all 14 small boxes in the second medium box in the third big box.

Quite a mental image, isn't it? Let's simplify a little bit by translating to Java Code:

What did we lean about multi-dimensional arrays from this? Take a look at lines 12-14. (In Java) A 3-dimensional array is nothing more than a 1-dimensional array that contains 2-dimensional arrays. So, in this example, the length of our 3-Dimensional array is 12, becuase it is a 1-Dimensional array that can contain 12 2-dimensional arrays. Likewise, the length of the 2-dimensional array that is the first sub-array (a3DArray[0]) is 9, because is is a 1-dimensional array that can contain 9 1-dimensional arrays.

Finally, note that a3DArray[10] is null. We didn't put any medium boxes in there, so there's nothing there. Also note that we can't put any figurines or small boxes in the big box; the compiler prevents us. Only medium boxes (2-D arrays) can go in the big box.

So, in Summary:

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

As Joel so aptly illustrates, a multi-dimensional array is just an array of arrays of arrays of arrays of arrays.... To make it simple, let's just use a 2D array (i.e. an array of arrays):

Using this example array, a.length is the number of 1D arrays there are in the 2D array. You can think of this as the number of rows. Then if you do a[i].length, you will get the number of elements in "row" i. Typically, each row has the same number of elements, but as Joel described, you don't have to do this. For now, I suggest that you keep things simple and use "rectangular" multi-dimensional arrays. When you have mastered that then you can move on to arrays where the each "row" has a different length.

Keep Coding!

Layne

Using this example array, a.length is the number of 1D arrays there are in the 2D array. You can think of this as the number of rows. Then if you do a[i].length, you will get the number of elements in "row" i. Typically, each row has the same number of elements, but as Joel described, you don't have to do this. For now, I suggest that you keep things simple and use "rectangular" multi-dimensional arrays. When you have mastered that then you can move on to arrays where the each "row" has a different length.

Keep Coding!

Layne

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |