views:

677

answers:

12

How have you explained nested arrays to a programmer. I'm thinking someone that has an entry level understanding of programming, but is trying to do more complicated coding.

The array with array works, but they can't quite get their mind around the idea.

Edit: example of a nested array:

array(
    'array1' => array(
        'key1' => 'val1',
        'key2' => 'val2',
    ),
    'array2' => array(
        'key1' => 'val1',
        'key2' => 'val2',
    ), 
);

Of course, they are usually more complicated than this and maybe that's the problem.

+7  A: 

Tell them to think of an array as a list- it helps to give them something less abstract, like a grocery list. Then, a nested array is simply a list of lists.

Maybe I have a todo list, a grocery list, and a wishlist at amazon.com . Now I have a list of all of my lists, and I can look at all of those elements in each list by stepping through them.

Tim Howland
+2  A: 

How have you explained it? It doesn't seem like a big jump for someone that understands one dimensional arrays to be able to grasp the concept that instead of an int or a string that each array element contains another array instead.

Perhaps an analogy comparing directories will help, a one dimensional array would be analogous to a directory that contains a bunch of files, a two-dimensional array to a directory which contains several other directories, each containing a bunch of files, etc.

Robert Gamble
A: 

If you are looking at C type, non-ragged, arrays, comparing it to numbers, the base 10 part, and there digits might help. Another good source for this same effect would be time as it has a non uniform base 60s = 1m, 60m = 1h, 24h = 1day, 7day = 1week

BCS
+1  A: 

Use a bitmap as an example. In C, you can make a bitmap of an X like this:

int x[5][5] = {
    { 1,0,0,0,1 },
    { 0,1,0,1,0 },
    { 0,0,1,0,0 },
    { 0,1,0,1,0 },
    { 1,0,0,0,1 }
};

Then show them how to use nested for loops to display the bitmap.

Examples always help, and this also gets them to think of nested arrays as multi-dimensional arrays. Actually it's probably better to understand multi-dimensional arrays in a language like C before learning about the "nested" arrays in languages like Python where you can have different levels of nesting in the same array.

yjerem
you *can* do that in C, your compiler will just point out that perhaps casting your int to an int* in that section of code is not the best idea you'e ever come up with ;)
tloach
What do you mean? This is the correct way of initializing a multi-dimensional array, I use it all the time and my compiler doesn't say anything about it.
yjerem
I just looked it up and can confirm that my code is completely OK. I can see why you might think that "{ 1,0,0,0,1 }" evaluates to a pointer to an int, but actually the inner braces in the initialization are only there for readability. Writing it without the braces would have the exact same effect.
yjerem
+1  A: 

Sports can provide appropriate analogies to describe applying nested arrays. A team is an array of people, a competition is an array of teams that play against each other.

However its a case of finding the analogy that clicks with the learner. Find the right analogy and you'll get even the slowest of learners to understand. Just ensure you're analogies are water tight. Like abstractions, they are leaky.

Jim Burger
+3  A: 

A nested array is a set within a set. So, a library has a set of books, a book has a set of chapters. A chapter has a set of paragraphs, a paragraph has a set of sentences. A sentence has a set of words.

For each book in library

For each chapter in book

    For each paragraph in chapter

etc...

thursdaysgeek
+1  A: 

A concrete example is the index at the back of a book. A list of words, each word associated with a list of page numbers.

apples - 1, 2, 3-4
bears - 32-35, 79, 83
cats - 14, 15

Corey Trager
A: 

What I remember my teacher doing to explain multi dimensional arrays was to bring in a little portable index card file (Not unlike those old Dewey Decimal System cabinets that could be found in libraries some time ago).

Each slot was a 2 dimensional array (i.e. each index held an array of note cards). But the container itself was an index of a 3 dimensional array (i.e. each index held an array that held an array of notecards). Then from there the theory was easy.

Concrete examples are very useful.

SauceMaster
A: 

2 dimensions is easy to explain. Just think of a table. 3 dimensions just think of a cube or other 3d image. 4 dimensions think of a series of images like a movie with the 4th dimension being time.

4+ dimensions is hard to visualize using that model. But think of it as a filing cabinet with another file cabinet inside helps. You open the drawer and out pops a filing cabinet. You find the drawer you want and open that drawer and out pops another filing cabinet....over and over until finally you get your paper.

Cervo
+1  A: 

Draw it.

A variable is a box
1 dimensional array is a row of boxes.
2 dimensional array is a grid of boxes.
3 dimensional array is a cube of boxes.

If they have having trouble with the general concept, don't attempt to visually explain 4 dimensions.

acrosman
A: 

Perhaps you are explaining it from the context of someone who understands an array of arrays. I would attempt to trick them into realizing that they already understand them by starting at the smallest(read inner array)...and slowly expanding out, giving them plenty of time to ask questions until they are done.

Drawing helps, but you need to give the student in this case some information and go slowly, most programmers I know tend to go to fast and to like to explain things EVEN when the listener no longer is tracking what is being said.

I am a metaphor guy, so I would probably cook something up about a series of boxes with each one numbered, each box then containing a similiar(but much smaller series) also numbered. I would take this to only two levels get understanding and then perhaps talk about 3 dimensions for confirmation. But I would avoid 4 dimensions on the grounds that they may get hung in the idea that there is no such thing as 4 dimensions, or you can't measure time, or other such metaphorical landmines/distractions...cause that's the other problem, programmers tend to be ADD and enjoy getting side tracked.

Also why aren't you using a hash of hashes, much easier to reference. :)

Bottom line, baby steps.

A: 

an array is just an object - a thing. everything should be simple to understand once they get that

Scott Evernden