Array Dimensions in Visual BasicArray Dimensions in Visual Basic

維度是一種可以改變陣列元素規格的方向。A dimension is a direction in which you can vary the specification of an array's elements. 保存當月每一天之銷售總額的陣列, 有一個維度 (當月的第一天)。An array that holds the sales total for each day of the month has one dimension (the day of the month). 每個月的銷售總額依部門保存的陣列有兩個維度 (部門編號和月份的日期)。An array that holds the sales total by department for each day of the month has two dimensions (the department number and the day of the month). 陣列所擁有的維度數目稱為其次序The number of dimensions an array has is called its rank.

注意

您可以使用Rank屬性來判斷陣列具有多少維度。You can use the Rank property to determine the how many dimensions an array has.

使用維度Working with Dimensions

您可以為每個維度提供索引或注, 藉以指定陣列的元素。You specify an element of an array by supplying an index or subscript for each of its dimensions. 元素是從索引0到該維度的最高索引的每個維度的連續。The elements are contiguous along each dimension from index 0 through the highest index for that dimension.

下圖顯示具有不同排名之陣列的概念結構。The following illustrations show the conceptual structure of arrays with different ranks. 圖例中的每個元素都會顯示存取它的索引值。Each element in the illustrations shows the index values that access it. 例如, 您可以藉由指定索引(1, 0)來存取二維陣列中第二個數據列的第一個元素。For example, you can access the first element of the second row of the two-dimensional array by specifying indexes (1, 0).

顯示一維陣列的圖表。

顯示二維陣列的圖表。

顯示三維陣列的圖表。

一個維度One Dimension

許多陣列只有一個維度, 例如每個年齡的人員人數。Many arrays have only one dimension, such as the number of people of each age. 唯一指定專案的需求是該元素持有計數的年齡。The only requirement to specify an element is the age for which that element holds the count. 因此, 這類陣列只會使用一個索引。Therefore, such an array uses only one index. 下列範例會宣告一個變數, 以保留一維的年齡計數陣列, 用於年齡0到120。The following example declares a variable to hold a one-dimensional array of age counts for ages 0 through 120.

Dim ageCounts(120) As UInteger

兩個維度Two Dimensions

有些陣列有兩個維度, 例如校園上每個建築物的每個樓層的辦公室數目。Some arrays have two dimensions, such as the number of offices on each floor of each building on a campus. 元素的規格需要建築物編號和樓層, 而且每個專案都包含該建築物和樓層的計算計數。The specification of an element requires both the building number and the floor, and each element holds the count for that combination of building and floor. 因此, 這類陣列會使用兩個索引。Therefore, such an array uses two indexes. 下列範例會宣告一個變數, 以保存一維的 office 計數陣列, 適用于建築物0到 40, 而樓層0到5的一個。The following example declares a variable to hold a two-dimensional array of office counts, for buildings 0 through 40 and floors 0 through 5.

Dim officeCounts(40, 5) As Byte

二維陣列也稱為「矩形陣列」。A two-dimensional array is also called a rectangular array.

三個維度Three Dimensions

幾個陣列有三個維度, 例如3d 空間中的值。A few arrays have three dimensions, such as values in three-dimensional space. 這類陣列使用三個索引, 在此案例中代表實體空間的 x、y 和 z 座標。Such an array uses three indexes, which in this case represent the x, y, and z coordinates of physical space. 下列範例會宣告一個變數, 以在三維磁片區中的不同點上保留一維空氣溫度陣列The following example declares a variable to hold a three-dimensional array of air temperatures at various points in a three-dimensional volume.

Dim airTemperatures(99, 99, 24) As Single

三個以上的維度More than Three Dimensions

雖然陣列最多可以有32個維度, 但很少會有三個以上的大小。Although an array can have as many as 32 dimensions, it is rare to have more than three.

注意

當您將維度新增至陣列時, 陣列所需的總儲存空間會大幅增加, 因此請小心使用多維陣列。When you add dimensions to an array, the total storage needed by the array increases considerably, so use multidimensional arrays with care.

使用不同的維度Using Different Dimensions

假設您想要追蹤目前月份每天的銷售金額。Suppose you want to track sales amounts for every day of the present month. 您可以宣告一維陣列, 其中包含31個元素, 每個月一天一個, 如下列範例所示。You might declare a one-dimensional array with 31 elements, one for each day of the month, as the following example shows.

Dim salesAmounts(30) As Double

現在假設您想要追蹤相同的資訊, 而不只是每個月的一天, 而且每個月都有。Now suppose you want to track the same information not only for every day of a month but also for every month of the year. 您可以宣告具有12個數據列 (適用于月份) 和31個數據行 (代表天) 的二維陣列, 如下列範例所示。You might declare a two-dimensional array with 12 rows (for the months) and 31 columns (for the days), as the following example shows.

Dim salesAmounts(11, 30) As Double

現在假設您決定讓陣列保存超過一年的資訊。Now suppose you decide to have your array hold information for more than one year. 如果您想要追蹤5年的銷售量, 您可以宣告具有5個圖層、12個數據列和31個數據行的三維陣列, 如下列範例所示。If you want to track sales amounts for 5 years, you could declare a three-dimensional array with 5 layers, 12 rows, and 31 columns, as the following example shows.

Dim salesAmounts(4, 11, 30) As Double

請注意, 因為每個索引的大小從0到最大值, salesAmounts所以的每個維度都會宣告為小於該維度所需的長度。Note that, because each index varies from 0 to its maximum, each dimension of salesAmounts is declared as one less than the required length for that dimension. 另請注意, 陣列的大小會隨著每個新的維度而增加。Note also that the size of the array increases with each new dimension. 上述範例中的三個大小分別為31、372和1860個元素。The three sizes in the preceding examples are 31, 372, and 1,860 elements respectively.

注意

您可以建立陣列, 而不使用Dim語句New或子句。You can create an array without using the Dim statement or the New clause. 例如, 您可以呼叫CreateInstance方法, 或另一個元件可以將以這種方式建立的陣列傳遞給您的程式碼。For example, you can call the CreateInstance method, or another component can pass your code an array created in this manner. 這類陣列可以具有小於0的下限。Such an array can have a lower bound other than 0. 您一律可以使用GetLowerBound方法LBound或函數, 測試維度的下限。You can always test for the lower bound of a dimension by using the GetLowerBound method or the LBound function.

另請參閱See also