Difference between revisions of "G11ntest/Documentation/BASIC Guide/Arrays"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Created page with '{{文档//v2 | | |/Basic Guide/日期 |/Basic Guide/范围的变量 | }} {{DISPLAYTITLE:Arrays}} ____ 除了简单变量(标量)、 {{OOo}} Basic还支持阵列(数据字段…')
 
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{文档//v2  
+
{{Documentation/BASICGuideTOC/v2
|
+
|ShowPrevNext=block
|
+
|ShowPrevPage=block
|/Basic Guide/日期
+
|PrevPage=Documentation/BASIC Guide/Date
|/Basic Guide/范围的变量
+
|NextPage=Documentation/BASIC Guide/Scope of Variables
|
+
|lang=block
 
}}
 
}}
 
{{DISPLAYTITLE:Arrays}}
 
{{DISPLAYTITLE:Arrays}}
  
____
+
{{OOo}}
除了简单变量(标量)、 {{OOo}} Basic还支持阵列(数据字段)。 数据字段含有多个变量,这些问题已通过一个索引。
+
  
  
=定义阵列=  
+
==
阵列可以定义如下:
+
  
  
  
 +
====
  
  
数组声明与将一个简单变量声明。 但是,与变量声明,则阵列名称后面括号,其中含有规范的元素数目。 以下表达式
 
  
  
lang="">
 
Dim MyArray(3)
 
</source>
 
  
 +
<source lang="oobas">
 +
Dim MyArray(3)
 +
</source>
  
将声明数组中有四个变量变体数据类型,即 <tt>MyArray(0)</tt>、 <tt>MyArray(1)</tt>、 <tt>MyArray(2)</tt>和 <tt>MyArray(3)</tt>。
 
  
 +
<tt>MyArray(0)</tt><tt>MyArray(1)</tt><tt>MyArray(2)</tt><tt>MyArray(3)</tt>
  
您也可以声明特定类型的变量。 例如,以下的动态LDAP URL返回经理为johndoe的所有用户:行声明一个阵列具有四个整数变量:
 
  
  
lang="">
 
Dim MyInteger(3)As Integer
 
</source>
 
  
  
在上面的示例,则该索引的阵列总是以标准起始值为零。 作为一种解决方法是,将有效性区域的开始和结束值可以是指定的数据字段声明。 以下示例声明一个含有六个整数值的数据字段,而且该数据字段可使用索引5到10进行寻址:
+
<source lang="oobas">
 +
Dim MyInteger(3) As Integer
 +
</source>
  
  
lang="">
 
Dim MyInteger(5至10)As Integer
 
</source>
 
  
  
文档索引不需要是正值。 以下示例也显示了一个正确声明,但具有负的数据字段限制值:
 
  
 +
<source lang="oobas">
 +
Dim MyInteger(5 To 10) As Integer
 +
</source>
  
lang="">
 
Dim MyInteger(-10 -5)As Integer
 
</source>
 
  
  
它声明一个整数数据字段值6可以使用索引 -10 -5。
 
  
  
可以使用三种限制时必须遵守定义数据字段索引:
+
<source lang="oobas">
 +
Dim MyInteger(-10 To -5) As Integer
 +
</source>
  
  
* 可能的最小索引为-32768。
 
* 可能的最大索引为32767。
 
* "最大的元素数目(在一个数据字段维数中)为16368。
 
  
  
{{Documentation/Note|Other limit values sometimes apply for data field indexes in VBA. The same also applies to the maximum number of elements possible per dimension. The values valid there can be found in the relevant VBA documentation.}}
 
  
  
  
值启动
 
  
 +
<source lang="oobas">
 +
Dim s(-53000 to 89000) As String
 +
s(-52000) = "aa"
 +
s(79999) = "bb"
 +
print s(-52000), s(79999)
 +
</source>
  
的起始索引数据字段通常以值0。 或者,您可以更改的起始索引所有数据字段声明为值1使用调用:
 
  
 +
{{Note|}}
  
lang="">
 
Option Base1
 
</source>
 
  
 +
====
  
调用必须包含在标题模块(如果您希望它适用于所有数组声明该模块中。 但是,此调用不影响UNO序列,该通过 {{OOo}} API的索引始终从0开始。 要使程序更清晰,您应该避免使用Option Base1。
 
  
  
元素的数目的数组不受影响,如果您使用 <tt>Option Base1</tt>,只有起始索引会发生变化。 在声明
 
  
  
lang="">  
+
<source lang="oobas">
Option Base1
+
Option Base 1
 +
</source>
 +
 
 +
 
 +
{{OOo}}
 +
 
 +
 
 +
<tt>Option Base 1</tt>
 +
 
 +
 
 +
<source lang="oobas">
 +
Option Base 1
 
' ...
 
' ...
Dim MyInteger(3)  
+
Dim MyInteger(3)
</source>  
+
</source>
  
  
创建4个整数变量,可说,表达式 <tt>MyInteger(1)</tt><tt>MyInteger(2)</tt><tt>MyInteger(3)</tt><tt>MyInteger(4)</tt>
+
<tt>MyInteger(1)</tt><tt>MyInteger(2)</tt><tt>MyInteger(3)</tt><tt>MyInteger(4)</tt>
  
  
{{Documentation/Note|In {{OOo}} 基本验证、表达式 <tt>Option Base1</tt> 不影响的元素数目的阵列中与VBA不同。 它只是使中的起始索引发生改变 {{OOo}} Basic。 虽然这项宣言 <tt>MyInteger(3)</tt> 建立三个整数值在VBA中,索引1到3,该声明的 {{OOo}} 基本创建四个整数值的索引1到4。 使用 <tt>选项兼容</tt>, {{OOo}} Basic行为与VBA。 }}
+
{{Note|{{OOo}}<tt>Option Base 1</tt>{{OOo}}<tt>MyInteger(3)</tt>{{OOo}}<tt>Option Compatible</tt>{{OOo}}}}
  
  
维数据
+
====
  
  
除了单维数据字段之外, {{OOo}} Basic还支持使用多维数据字段。 相应的维之间用逗号分隔。 示例
+
{{OOo}}
  
  
lang="">  
+
<source lang="oobas">
Dim MyIntArray(5,5)As Integer  
+
Dim MyIntArray(5, 5) As Integer
</source>  
+
</source>
  
  
定义一个整型数组维数两个,每个6个索引(进行寻址通过索引0到5)。 整个数组总共可记录6x6=36个整数值。
+
=
  
  
您可以定义成百上千的维数的 {{OOo}} Basic数组,但可以使用的内存数量限制了的维数,可以设定。
+
{{OOo}}
  
  
更改尺寸数据
+
====
  
  
在上面的实例中的数据字段,指定维数。 您也可以定义中的数组的维数的数据字段进行动态更改。 例如,您可以定义一个数组包含所有的字词文字以字母A的数量的这些字词最初不知道,需要以后能够更改字段限制。 为此,在 {{OOo}} Basic中使用以下调用:
+
{{OOo}}
  
  
lang="">  
+
<source lang="oobas">
ReDim MyArray(10)  
+
ReDim MyArray(10)
</source>  
+
</source>
  
  
{{Documentation/Note|Unlike VBA, where you can only dimension dynamic arrays by using <tt>Dim MyArray()</tt>, {{OOo}} Basic允许您更改静态和动态数组使用 <tt>ReDim</tt>}}
+
{{Note|<tt>Dim MyArray()</tt>{{OOo}}<tt>ReDim</tt>}}
  
  
以下示例更改初始数组的维数,这样该数组就可以记录11或21个值:
 
  
  
lang="">  
+
 
Dim MyArray(4) As Integer'带有五个元素的声明
+
<source lang="oobas">
 +
Dim MyArray(4) As Integer ' Declaration with five elements
 
' ...
 
' ...
ReDim MyArray(10)As Integer'增加到11个元素
+
ReDim MyArray(10) As Integer ' Increase to 11 elements
 
' ...  
 
' ...  
ReDim MyArray(20)As Integer'增加到21个元素
+
ReDim MyArray(20) As Integer ' Increase to 21 elements
</source>  
+
</source>
  
  
当您重置维数组时,您可以使用任何选项列出了上述一节。 这包括声明多维数据字段和指定明确的起始值和最终值。 的维数的数据字段进行更改后,所有内容都将丢失。 如果要保留原始数值,请使用 <tt>Preserve</tt> 命令:
+
<tt>Preserve</tt>
  
  
lang="">  
+
<source lang="oobas">
Dim MyArray(10)As Integer'定义初始
+
Dim MyArray(10) As Integer ' Defining the initial
'维数
+
' dimensions
 
' ...  
 
' ...  
ReDim Preserve MyArray(20)As Integer'增加
+
ReDim Preserve MyArray(20) As Integer ' Increase in
"数据字段,
+
' data field, while
'保留内容
+
' retaining content
</source>  
+
</source>
 +
 
 +
 
 +
<tt>Preserve</tt>
 +
 
 +
 
 +
{{Note|<tt>Preserve</tt>{{OOo}}}}
 +
 
 +
 
 +
<tt>ReDim</tt><tt>Preserve</tt>
 +
 
 +
 
 +
====
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
<source lang="oobas">
 +
Dim MyArray(10) As Integer
 +
' ... some instructions
 +
Dim n As Integer
 +
n = 47 ' could be the result of a computation
 +
Redim MyArray(n) As Integer
 +
MsgBox(LBound(MyArray))  ' displays : 0
 +
MsgBox(UBound(MyArray))  ' displays : 47
 +
</source>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
<source lang="oobas">
 +
Dim MyArray(10, 13 to 28) As Integer
 +
MsgBox(LBound(MyArray, 2))  ' displays : 13
 +
MsgBox(UBound(MyArray, 2))  ' displays : 28
 +
</source>
 +
 
 +
 
 +
====
 +
 
 +
 
 +
 
 +
 
 +
 
 +
<source lang="oobas">
 +
Dim s() As String ' declare an empty array
 +
' --- later in the program ...
 +
Redim s(13) As String
 +
</source>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
<source lang="oobas">
 +
Dim MyArray() As Integer
 +
MsgBox(LBound(MyArray))  ' displays : 0
 +
MsgBox(UBound(MyArray))  ' displays : -1
 +
</source>
 +
 
 +
 
 +
 
 +
 
 +
 
 +
==
  
  
当您使用 <tt>Preserve</tt>时,请确保维数和变量类型保持不变。
 
  
  
{{Documentation/Note|Unlike VBA, where only the upper limit of the last dimension of a data field can be changed through <tt>Preserve</tt>, {{OOo}} Basic允许您更改其他维。 }}
 
  
 +
=
  
如果您使用 <tt>ReDim</tt> 与 <tt>保留</tt>,必须使用相同数据类型的原始数据字段声明。
 
  
 +
==
  
=定义值的阵列=
 
  
  
值的阵列字段可以存储所示:
 
  
  
MyArray(0)="someValue"
 
  
  
=访问阵列=
 
  
 +
==
  
访问值的阵列功能类似于以下内容:
 
  
  
"值:"&MyArray(0))
 
  
  
=ARRAY创建、值赋值和访问示例=
+
=
和示例包含所有步骤,显示实际阵列使用情况:
+
  
  
 
Dim MyArray(3)
 
MyArray(0)="拉拉"
 
"值:"&MyArray(0))
 
End Sub
 
  
  
Line 201: Line 260:
  
  
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Arrays}}
+
{{InterWiki Languages BasicGuide|articletitle=}}
 +
{{PDL1}}
  
{{PDL1}}[[Category:G11n Wiki Translation Testing]]
+
[[Category:G11n Wiki Translation]]

Latest revision as of 07:38, 7 July 2018


Apache OpenOffice


==


==

Dim MyArray(3)


MyArray(0)MyArray(1)MyArray(2)MyArray(3)



Dim MyInteger(3) As Integer



Dim MyInteger(5 To 10) As Integer



Dim MyInteger(-10 To -5) As Integer





Dim s(-53000 to 89000) As String
s(-52000) = "aa"
s(79999) = "bb"
print s(-52000), s(79999)


Documentation note.png


==

Option Base 1


Apache OpenOffice


Option Base 1


Option Base 1
' ...
Dim MyInteger(3)


MyInteger(1)MyInteger(2)MyInteger(3)MyInteger(4)


Documentation note.png Apache OpenOfficeOption Base 1Apache OpenOfficeMyInteger(3)Apache OpenOfficeOption CompatibleApache OpenOffice


==

Apache OpenOffice


Dim MyIntArray(5, 5) As Integer


=


Apache OpenOffice


==

Apache OpenOffice


ReDim MyArray(10)


Documentation note.png Dim MyArray()Apache OpenOfficeReDim



Dim MyArray(4) As Integer ' Declaration with five elements 
' ...
ReDim MyArray(10) As Integer ' Increase to 11 elements
' ... 
ReDim MyArray(20) As Integer ' Increase to 21 elements


Preserve


Dim MyArray(10) As Integer ' Defining the initial 
' dimensions
' ... 
ReDim Preserve MyArray(20) As Integer ' Increase in 
' data field, while
' retaining content


Preserve


Documentation note.png PreserveApache OpenOffice


ReDimPreserve


==

Dim MyArray(10) As Integer
' ... some instructions
Dim n As Integer
n = 47 ' could be the result of a computation
Redim MyArray(n) As Integer
MsgBox(LBound(MyArray))  ' displays : 0
MsgBox(UBound(MyArray))  ' displays : 47



Dim MyArray(10, 13 to 28) As Integer
MsgBox(LBound(MyArray, 2))  ' displays : 13
MsgBox(UBound(MyArray, 2))  ' displays : 28


==

Dim s() As String ' declare an empty array
' --- later in the program ...
Redim s(13) As String





Dim MyArray() As Integer
MsgBox(LBound(MyArray))  ' displays : 0
MsgBox(UBound(MyArray))  ' displays : -1



==



=


==





==



=


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools