views:

234

answers:

6

I am having trouble wrapping my head around the concept of an array with more than two dimensions, why you would need one, and how you would use it.

For example, how would you go about representing the following data in a multidimesional array?

Sex: Male | Female
Hair Color: Blond | Brunette | Black
Eye Color: Blue | Brown | Green | Hazel

Instinct is telling me that I should create an array as so:

string[,,] personAttributes = new string[2,3,4]

Please show how you would you fill up this array without a loop, and then with a loop. Any expansion on concepts and usage appreciated.

+1  A: 

To model data structures that have multiple dimensions. A chess board is a good example, 1 dimension is for rank, the other is for file.

Since the categories of data in your example (gender, eye color, hair color) have no relation with other categories, it seems that this would be best represented as 3 different arrays, each with 1 dimension.

If you do want to loop over a multi-dimension array, you simply use a loop within a loop:

for (int i = 0; i < array.length; i++) {
   for (int j = 0; j < array[0].length; j++) {
      string data = array[i][j];
      // do something with the data
   }
}
Kaleb Brasee
*more* than two?
Artelius
More than 2 what?
Kaleb Brasee
+7  A: 

At the risk of sounding trite, you use arrays of three or more dimensions when you have three or more dimensions of data. So I guess you've having trouble envisioning three dimensions of data.

How about 3-D Tic Tac Toe? Any discrete representation of three-dimensional data fits in this category.

As for attributes like hair colour and so on, I wouldn't use a multi-dimensional array for this. Use objects with properties for that and enums (eg gender as an enum) as appropriate. That'll be much more readable than an N-dimensional array.

cletus
right, not trite.
Stuart
+2  A: 

I'd say in your example a multi-dimensional array does not make sense. A class makes much more sense in your situation. Something like an enumeration stored as a member variable would be one way you could go:

enum HAIRCOLORS { BROWN = 0, BLOND = 1 ..... };
enum SEX { FEMALE = 0, MALE = 1 };
enum EYECOLORS { GREEN, BLUE, RED .... };

class PersonAttributes 
{
    public SEX sex = SEX.Female;
    public HAIRCOLORS hairColor = HAIRCOLORS.Brown;
    public EYECOLORS eyeColor = EYECOLORS.Green;
};

etc...

Polaris878
+7  A: 

I'm not going to touch your personAttributes example because I don't think a 2D array is a good idea, let alone 3D (personally I would use an array of structs).

However, multidimensional arrays are very useful when you have some kind of orthogonal data space (i.e. you have several "choices" which are independent of each other).

For instance, if you're storing the response times of 20 people over 10 tests, where each test is repeated 3 times, and the whole thing is done once a month for 12 months, you might have an array like this:

double[,,,] responseTime = new double [12,20,10,3];
Artelius
Note, C# supports "jagged" arrays, declared of the form double[][][][] arrayName. This for instance would allow you to have different numbers of people per month, a different number of tests per person in each month, and so on. Such arrays are slower to index, however.
Artelius
+1  A: 

Thinking of an array as an address may be helpful.

123 Main St Springfield MA

Using this example, my first array would be an array of states. Each state would hold an array of cities, than cities holds streets and finally streets holds individual addresses.

With this array we could easily create a mailing list with every address. Just loop through each array and you would be able to print out every address or whatever you need to do.

Looking at your example, I don't see multidimensional arrays as a good fit. Unless the main thing you want to do with your arrays is find subsets of your data, like people that are female / blond / blue eyes. I would follow the suggestion to use a class. When you are looking at a person object in an array you are going to need to know the index values pointing to that person to figure out those characteristics.

Another example that might be useful is internationalization of messages in an application. The arrays could be language, state (error, warning, info), message id (an array message strings).

As for filling the arrays, just a few for loops could be used if the data was sorted. Otherwise parse the your input data to identify the appropriate indices.

Philip T.
Oddly enough, my dad used to work extremely close to your example address. True story.
Jesse C. Slicer
A: 

As the others wrote, your example doesn't well suited to a 3D array. Your example seem more appropriate to a 2D data structure. One index is persons, the other index is the characteristic: sex, hair color, eye color. Or you could use some other data structure...

A simple example of a 3D array: considering storing an (uncompressed) black & white digital movie. Each frame is a 2D image X vs Y with intensity values: image (i,j). Now to have multiple frames to have a movie you could store the movie as images (i, j, k), where k changes over time. If the movie was in color, you could add a fourth dimension to store three primary colors: cimages (i, j, q, k), q=1,2,3, and have a 4D array.

M. S. B.