简体   繁体   中英

What do we mean by "dimension" when talking about arrays?"

What do we mean by "dimension" when talking about arrays?

I understand the idea. But what would be the answer?

For example, int array[5]; I know that, this is a 1D array. It has 1 subscript in the index. But why is it called a 1 Dimensional array? Why not 1 Subscript array?

We say "dimension" because that's the general term for this sort of thing. Think about our world, for instance: It has three readily-apparent dimensions (width, height, depth). Or think of geometry: A point has zero dimensions, a line has one, a plane has two, a cube has three, etc. The terminology applies to arrays because it precisely describes the same thing in relation to the array. The dimensionality of an array is how many axes it has.

A one dimensional array has one axis, like a line:

XXXXXXXX

You index into it with one subscript, eg array[n] .

A two-dimensional array has two axes, like a plane:

XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX

You index into it with two subscripts, eg array[x,y] .

I won't attempt to represent 3+ dimensional arrays (like cubes) with ASCII art. :-)

"Dimension of an Array" is the number of indices, or subscripts, that you need in order to specify an individual element of the array.

Dimensions and subscripts may be confusing. Subscript is a number (or another type of associative key), while dimension is a description of the range of acceptable keys; you need one subscript for each dimension of the array.

For example, in C/C++ a[10][5] is an array with two dimensions: of size 10 and of size 5. You need two subscripts, or keys, to address its elements. One subscript has to be between 0 and 9, inclusive; the other subscript is between 0 and 4.

The simplest way to think of it is that a dimension of an array is the number of square brackets that follow the type:

int[] is a single dimension array, int[][] is a 2 dimensional array, etc.

Sometimes it is helpful if you think of an array as if you were graphing them in multiple dimensions. A 1-d array is simply a line and has 1 axis in a graph. A 2-d array is a table and has two axis if you were to graph it (x,y). A 3d array is a cube and would have 3 axis (x,y,z).

Dimension apply pretty much in the same way to an array as it does not cartesian coordinate system. Dimension means in how many axis an array can grow. Example

int A[5]  is one dimentional, all elements are access by one index
int A[5][5] is two dimentional. Element are aligned along x and y plane.
int A[5][5][5] is three diminutional, elements are aligned in 3D space.

As you grow up 3D, the visual becomes difficult. A 4d Array would be like this

int A[5][5][5][5]

that essentially means it can grow in 4 directions. It can be visualized as

[5][5][5]  [5][5][5] [5][5][5] [5][5][5] [5][5][5] [5][5][5]

[5][5][5]  [5][5][5] [5][5][5] [5][5][5] [5][5][5] [5][5][5]

[5][5][5]  [5][5][5] [5][5][5] [5][5][5] [5][5][5] [5][5][5]

[5][5][5]  [5][5][5] [5][5][5] [5][5][5] [5][5][5] [5][5][5]

[5][5][5]  [5][5][5] [5][5][5] [5][5][5] [5][5][5] [5][5][5]

which is 5 elements of 3D array A[5][5][5]. In the above case it is only the 0th element of A[0][5][5][5]

I had a hard time grasping the idea for different dimensions and how they would pan out.

So, after reading the response by Sergey and reading the Microsoft doc on arrays I came up with the following script to visualize it:

Sub Main()
Worksheets("Sheet1").Range("A1", "AK127").Clear  ' Cleaning the array range

For i = 1 To 3
    For j = 1 To 4
        For k = 1 To 5
            Worksheets("Sheet1").Cells(1, ((k - 1) * 5) + 1).Offset((i), (j)).Value = i & "+" & j & "-" & k   ' Assigning numbers 
            Worksheets("Sheet1").Cells(1, ((k - 1) * 5) + 1).Offset((i), (j)).Interior.Color = RGB(255 / j, 155, 150)    ' Assigning colors
        Next k
    Next j
Next i



End Sub

It will spit out 5 blocks (k 1 to 5) of 3 by 4 arrays (i by j) with color schemes.

I hope this was a helpful visualization. One could play around with the numbers or i,j,k parameters to see how they change appearance.

A multi-dimensional array is one that allows its members to be arrays, too. For example :

a = [1, 2, 3]; // single dimension array b = [7, 8, 9]; // single dimension array

c = [a, b]; // multidimension (2-dimension) array. An array of arrays.

So now c[0] is assigned the array a as its element, and c[1] is b. Members of a multidimensional array can be addressed such as:

c[0][0] (which would be a[0] in this case, or 1... c[1][2] (which would be b[2] in this case, or 9...

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