Why use an array with more than two dimensions?

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

For example, how could you represent the following data in a multiple array?

Gender: Male | Female
Hair color: blond | Brunette | Black
Eye color: blue | Brown | Green | Hazel

Instinct tells me that I should create an array like this:

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

Please show how you would fill this array without a loop and then with a loop. Any expansion of concepts and uses was appreciated.

+2


source to share


6 answers


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

However, multidimensional arrays are very useful when you have some kind of orthogonal data space (ie you have several "options" that are independent of each other).



For example, if you keep the response time of 20 people over 10 tests, where each test is repeated 3 times, and this is all done once a month for 12 months, you could have an array like this:

double[,,,] responseTime = new double [12,20,10,3];

      

+8


source


At the risk of sounding clichΓ©, you use arrays of three or more dimensions when you have three or more dimensions of your data. So my guess is that you are having trouble representing the three dimensions of your data.

How about a 3-D Tic Tac Toe ? Any discrete representation of 3D data falls into this category.



As far as attributes like hair color etc. are concerned, I would not use a multidimensional array for this. Use objects with properties for this and enumeration (for example gender as an enum) if needed. This will be much more readable than an N-dimensional array.

+8


source


I would say that in your example, a multidimensional array doesn't make sense. The class makes a lot more sense in your situation. Something like an enum stored as a member variable would be one way:

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...

+2


source


To model data structures that have multiple dimensions. A good example is a checkerboard, 1 size for rank, another size for file.

Since the data categories in your example (gender, eye color, hair color) are unrelated to other categories, it seems like this would be best represented as three different arrays, each with 1 size.

If you want to iterate over a multidimensional array, you just use a loop in 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
   }
}

      

+1


source


It can be helpful to think of an array as an address.

123 Main St Springfield MA

Using this example, my first array will be an array of states. Each state will have an array of cities than the cities have streets, and finally, the streets have individual addresses.

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

Looking at your example, I don't see multidimensional arrays as a good fit. If the main thing you want to do with your arrays is to find subsets of your data, like people who are female / light / blue eyes. I would suggest using the class. When you look at a person object in an array, you will need to know the index values ​​that point to that person in order to figure out these characteristics.

Another example that might be helpful is internationalizing messages in an application. Arrays can be language, state (error, warning, information), message identifier (array message strings).

As far as filling the arrays, multiple loops can be used to sort the data. Otherwise, analyze your input to determine the appropriate indices.

+1


source


As others have posted, your example is not suitable for a 3D array. Your example seems more appropriate for a 2D data structure. One index is people, the other is character: gender, hair color, eye color. Or you can use a different data structure ...

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

0


source







All Articles