# 序列和数组映射

``` Dim a1( 100 ) ' Variant array, index range: 0-100 -> 101 elements

Dim a2%( 5 ) ' Integer array, index range: 0-5 -> 6 elements

Dim a3\$( 0 ) ' String array, index range: 0-0 -> 1 element

Dim a4&( 9, 19 ) ' Long array, index range: (0-9) x (0-19) -> 200 elements
```

Basic 中没有特殊的索引运算符，如 C++ 和 Java 中的 []。而是使用普通括号 () 访问数组元素：

``` Dim i%, a%( 10 )
for i% = 0 to 10           ' this loop initializes the array
a%(i%) = i%
next i%

dim s\$
for i% = 0 to 10           ' this loop adds all array elements to a string
s\$ = s\$ + " " + a%(i%)
next i%
msgbox s\$                  ' Displays the string containing all array elements

Dim b( 2, 3 )
b( 2, 3 ) = 23
b( 0, 0 ) = 0
b( 2, 4 ) = 24             ' Error ”Subscript out of range”
```

``` Dim i%, a%( 9 ) ' Maximum index 9 -> 10 elements
for i% = 0 to 9 ' this loop initializes the array
a%(i%) = i%
next i%

oSequenceContainer.TheSequence = a%()

' If “TheSequence” is based on XPropertySet alternatively
oSequenceContainer.setPropertyValue( “TheSequence”, a%() )
```

Basic 程序员在编程时，需要了解不同的索引语义。在以下示例中，程序员传送包含一个元素的序 列，但实际上传送了两个元素：

``` ' Pass a sequence of length 1 to the TheSequence property:
Dim a%( 1 )   ' WRONG: The array has 2 elements, not only 1!
a%( 0 ) = 3   ' Only Element 0 is initialized,
' Element 1 remains 0 as initialized by Dim

' Now a sequence with two values (3,0) is passed what
' may result in an error or an unexpected behavior!
oSequenceContainer.setPropertyValue( “TheSequence”, a%() )
```

 将 Basic 数组作为一个整体用于参数或用于属性访问时，在 Basic 代码中数组后面应始终跟有 '()'，否则，某些情况下可能会发生错误。

``` Dim a ' should be declared as Variant
a = Array( 1, 2, 3 )

' is the same as

Dim a(2)
a( 0 ) = 1
a( 1 ) = 2
a( 2 ) = 3
```

``` Dim a%() ' empty array/sequence of type Integer

Dim b\$() ' empty array/sequence of String
```

UNO 返回的序列在 Basic 中也表示为数组，但不必事先声明这些数组。应该将用于接受序列的变量声明为 `Variant`。要访问 UNO 返回的数组，需要获得有关 Basic RTL 函数 `LBound()``UBound()` 所包含的元素数目的信息。

``` Dim aResultArray ' should be declared as Variant
aResultArray = oSequenceContainer.TheSequence

dim i%, iFrom%, iTo%
iFrom% = LBound( aResultArray() )
iTo% = UBound( aResultArray() )
for i% = iFrom% to iTo% ' this loop displays all array elements
msgbox aResultArray(i%)
next i%
```

``` Dim a (3 to 5 )
```

``` Dim aResultArray ' should be declared as Variant
aResultArray = oSequenceContainer.TheSequence

Dim i%, iTo%
iTo% = UBound( aResultArray() )
For i% = 0 To iTo% ' this loop displays all array elements
MsgBox aResultArray(i%)
Next i%
```

``` u% = UBound( aResultArray() )
ElementCount% = u% + 1
```

``` ElementCount% = u% + 1' = -1 + 1 = 0
```

 UNO 序列与 Basic 数组之间的映射前提二者都使用一个基于零的索引系统。为了避免出现问题，不要使用语法 ``` Dim a ( IndexMin to IndexMin ) ``` 或 Basic 命令 `Option Base 1`。这二者使所有 Basic 数组的索引都不是从 0 开始。

UNO 还支持包含序列的序列。在 Basic 中，这对应于包含数组的数组。不要将包含序列的序列与多维数组相混淆。在多维数组中，所有子数组始终具有相同数目的元素，而在包含序列的序列中，每个序列可以具有不同的大小。示例：

``` Dim aArrayOfArrays ' should be declared as Variant
aArrayOfArrays = oExample.ShortSequences ' returns a sequence of sequences of short

Dim i%, NumberOfSequences%
Dim j%, NumberOfElements%
Dim aElementArray

NumberOfSequences% = UBound( aArrayOfArrays() ) + 1
For i% = 0 to NumberOfSequences% - 1' loop over all sequences
aElementArray = aArrayOfArrays( i% )
NumberOfElements% = UBound( aElementArray() ) + 1

For j% = 0 to NumberOfElements% - 1 ' loop over all elements
MsgBox aElementArray( j% )
Next j%
Next i%
```

``` ' Declare master array
Dim aArrayOfArrays( 2 )

' Declare sub arrays
Dim aArray0( 3 )
Dim aArray1( 2 )
Dim aArray2( 0 )

' Initialise sub arrays
aArray0( 0 ) = 0
aArray0( 1 ) = 1
aArray0( 2 ) = 2
aArray0( 3 ) = 3

aArray1( 0 ) = 42
aArray1( 1 ) = 0
aArray1( 2 ) = -42

aArray2( 0 ) = 1

' Assign sub arrays to the master array
aArrayOfArrays( 0 ) = aArray0()
aArrayOfArrays( 1 ) = aArray1()
aArrayOfArrays( 2 ) = aArray2()

' Assign the master array to the array property
oExample.ShortSequences = aArrayOfArrays()
```

``` ' Declare master array
Dim aArrayOfArrays( 2 )

' Create and assign sub arrays
aArrayOfArrays( 0 ) = Array( 0, 1, 2, 3 )
aArrayOfArrays( 1 ) = Array( 42, 0, -42 )
aArrayOfArrays( 2 ) = Array( 1 )

' Assign the master array to the array property
oExample.ShortSequences = aArrayOfArrays()
```

``` ' Declare master array variable as variant
Dim aArrayOfArrays

' Create and assign master array and sub arrays
aArrayOfArrays = Array( Array( 0, 1, 2, 3 ), Array( 42, 0, -42 ), Array( 1 ) )

' Assign the master array to the array property
oExample.ShortSequences = aArrayOfArrays()
```