Jagged Arrays in Visual Basic
An array of which each element is itself an array is called an array of arrays, or a jagged array. Note that having arrays as elements is not the same thing as a multidimensional array, which has more than one index on a single array.
Meaning of Jagged
Sometimes the data structure in your application is two-dimensional but not rectangular. For example, you might have an array of months, each element of which is an array of days. Since different months have different numbers of days, the elements do not form a rectangular two-dimensional array. In such a case, you can use a jagged array instead of a multidimensional array.
Example
The following example declares an array variable to hold an array of arrays with elements of the Double Data Type (Visual Basic). Each element of the array sales is itself an array that represents a month. Each month array holds values for each day in that month.
Dim sales()() As Double = New Double(11)() {} Dim month As Integer Dim days As Integer For month = 0 To 11 days = DateTime.DaysInMonth(Year(Now), month + 1) sales(month) = New Double(days - 1) {} Next month
The New clause in the declaration of sales sets the array variable to a 12-element array, each element of which is of type Double(), an array of Double elements. The For loop then determines how many days are in each month this year (Year(Now)), and sets the corresponding element of sales to a Double array of the appropriate size.
In the preceding example, the jagged array saves seven elements (six in a leap year) compared to a two-dimensional array. In a more extreme case the memory savings could be significant.
See Also
Tasks
How to: Declare an Array VariableHow to: Create an Array of Arrays
How to: Initialize a Jagged Array
Troubleshooting Arrays
Concepts
Overview of Arrays in Visual BasicArray Dimensions in Visual Basic
Multidimensional Arrays in Visual Basic
Array Data Types in Visual Basic
Writing CLS-Compliant Code
Other Resources
Arrays in Visual Basic
Community Additions
ADDA more straightforward example, that doesn't use functions
Private Sub Form1_Load(ByVal senderw As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'The following line declares a jagged array as an integer array with two arrays as its elements
Dim testarray()() As Integer = New Integer(1)() {}
'The following line declares the first array (array 0) as a new integer array.
testarray(0) = New Integer(1) {}
'The following line assigns the value of 12 to the first element of the first array of the set of arrays testarray.
testarray(0)(0) = 12
'When declaring sub arrays of a different data type, they must be able to be converted without loss to the parent data type, or you may get an error. This is especially the case, if you have enabled "option strict".
'메모' 카테고리의 다른 글
strsplit 분리자 정규식에서 escape backslash가 두개인 이유...펌 (0) | 2013.08.12 |
---|---|
vba checkbox name, value, checkbox_change()...펌 (0) | 2013.08.03 |
vba array 1차원 두개로 2차원 만들기...펌 (0) | 2013.08.01 |
[Tetrabromophenolphthalin] TBPE 검사...펌 (0) | 2013.07.31 |
vba sendkeys method...펌 (0) | 2013.07.31 |