简体   繁体   中英

Why use an Array of more than two dimensions?

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.

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.

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 (ie 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];

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

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

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.

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.

The easiest way of understanding a multidimensional array is to acknowledge every array as a one dimensional array. ie A 3 dimensional array is one dimensional array and every element in the one dimensional is a 2 dimensional array.

confusing........?

see this example

Here marks of a student named ABC in 2 exams conduct in the same year is displayed in a 2 dimensional array subject wise.

now when there is only one student it is easy to make a 2-D array but if you have to store marks of 3 students named ABC,DEF,GHI you can't do that in a 2D array. for this I will create a one dimensional array in which I can store the data of each student individually.

simplified.....

I will store the 2D array carrying data of each student in a one dimensional array and now that 1D is a 3 dimensional Array

Here Class Array is the 3 dimensional Array

Similarly a 4 dimensional array is also a 1 dimensional array where each element of that array is 3 dimensional array.

if you increase the number of attribute the dimension of the array will also increase and hence we can say that a multi dimensional array is used to store unrelated / orthogonal data in an organized manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM