<p>定义简单数组</p><p>有两种方法在asp中定义和初始化数组,让我们看看每种的例子:</p><p>方法一:</p><pre class="brush:as3;toolbar:false">MyArray=Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;)</pre><p>数组大小由初始化元素个数决定。</p><p>方法二:</p><pre class="brush:as3;toolbar:false">DimmyArray(2)&#39;指定数组大小 myArray(0)=&quot;Jan&quot; myArray(1)=&quot;Feb&quot;</pre><p>数组动态扩展</p><pre class="brush:as3;toolbar:false">DIMmyArray() REDIMmyArray(20)&#39;将数组大小重新定义为20 ReDimPreserveMyArray(i)&#39;Preserve保留数组中的原有数据</pre><p>二维数组</p><p>举例:</p><p>dim MyArray(5,10) &#39;定义了一个二维数组</p><p>二维赋值举例:</p><p>MYArray(3,3)=100</p><p>二维数组还有一种变相的实现方法:</p><pre class="brush:as3;toolbar:false">dimMyArray(5) MyArray(0)=Array(...)&#39;一维数组 MyArray(1)=Array(...)&#39;一维数组 ...</pre><p>访问的时候,用MyArray(x)(y)这样的格式</p><p></p><p>数组的下标</p><p>用上面的方法定义数组,每一维数组的第一个元素的下标是0,最后一个元素的下标就是元素数量-1</p><p>但也可以指定数组的下标,如:<br/></p><p>dim MyArray1(3 to 10) &#39;下标从3到10,MyArray(3)即获取第一个元素的值</p><p></p><p>有用的数组函数</p><p>Ubound(数组名)函数--返回数组的最后一个元素的下标。</p><p>Lbound(数组名)函数--返回数组的第一个元素的下标,缺省为0。</p><p>更多应用:</p><p>数组排序函数</p><pre class="brush:as3;toolbar:false">functionSort(ary) KeepChecking=TRUE DoUntilKeepChecking=FALSE KeepChecking=FALSE ForI=0toUBound(ary) IfI=UBound(ary)ThenExitFor Ifary(I)&gt;ary(I+1)Then FirstValue=ary(I) SecondValue=ary(I+1) ary(I)=SecondValue ary(I+1)=FirstValue KeepChecking=TRUE EndIf Next Loop Sort=ary Endfunction</pre><p>数组排序函数应用例子</p><pre class="brush:as3;toolbar:false">DimMyArray MyArray=Array(1,5,123,12,98) MyArray=Sort(MyArray) ForI=Lbound(MyArray)toUbound(MyArray) Response.WriteMyArray(I)&amp;&quot;&lt;br&gt;&quot; Next</pre><p><br/></p><p>将一个字符串分割并返回数组</p><pre class="brush:as3;toolbar:false">DimMyArray MyArray=Split(字符串,分割符) ForI=Lbound(MyArray)toUbound(MyArray) Response.WriteMyArray(I)&amp;&quot;&lt;br&gt;&quot; Next</pre><p><br/></p><p>在Application和Session中使用数组</p><pre class="brush:as3;toolbar:false">Application.Lock Application(&quot;StoredArray&quot;)=MyArray Application.Unlock</pre><p>LocalArray = Application(&quot;StoredArray&quot;)</p><p>覆盖Application中的数组</p><pre class="brush:as3;toolbar:false">Application.Lock Application(&quot;StoredArray&quot;)=LocalArray Application.Unlock</pre><p>Session使用方法与Application相同</p><p>从数据库中把数据导入数组中</p><pre class="brush:as3;toolbar:false">DimMyArray 取出全部记录 MyArray=RS.GetRows 取出前10项记录 MyArray=RS.GetRows(10) Forrow=0ToUBound(MyArray,2) Forcol=0ToUBound(MyArray,1) Response.Write(col,row)&amp;&quot;&lt;br&gt;&quot; Next Next</pre><p></p><p>向另一个页面传递数组</p><p>有很多种方法向另一页面传递数组,比如以下三种:</p><p>1、定义一个又逗号分隔的字符串,然后再下一页中用Split函数重新建立数组。</p><p>2、将数组存储在一个Session变量中,然后在下一个页面中调用。</p><p>3、通过表单的隐含区域来传递数组,他们都是自动用逗号分开,然后再用Split函数重新建立数组。</p><p>前两种方法很好,但是都比第三种复杂。在这里我们将只介绍第三种,因为它是最简单的。</p><p>文件1.asp:</p><pre class="brush:html;toolbar:false">&lt;% dimI dimmyArray(20) forI=0to20 myArray(I)=&quot;Item&quot;&amp;I next %&gt; &lt;html&gt; &lt;body&gt; &lt;formname=&quot;testform&quot;method=&quot;post&quot;action=&quot;2.asp&quot;&gt; &lt;% forI=0toubound(myArray) response.write&quot;&lt;inputtype=hiddenname=myArrayvalue=&#39;&quot;&amp;myArray(I)&amp;&quot;&#39;&gt;&quot; next %&gt; &lt;p&gt; &lt;inputtype=&quot;submit&quot;&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre><p>以上我们做的是在一个表单中用单独的隐含域存储数组中的每个元素,我们再看看下一页:</p><p>文件2.asp</p><pre class="brush:html;toolbar:false">&lt;html&gt; &lt;body&gt; &lt;% dimarrString dimmyArray dimI arrString=request(&quot;myArray&quot;) myArray=split(arrString,&quot;,&quot;) forI=0toubound(myArray) response.write&quot;Item&quot;&amp;I&amp;&quot;=&quot;&amp;myArray(I)&amp;&quot;&lt;br&gt;&quot;&amp;vbCrLf next %&gt; &lt;/body&gt; &lt;/html&gt;</pre>
T:0.006621s,M:251.98 KB
返回顶部 留言