标题:谁能帮我解释下:ASP二维数组
只看楼主
xuehao
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2011-1-18
结帖率:55.56%
已结贴  问题点数:20 回复次数:4 
谁能帮我解释下:ASP二维数组
<%
dim aa(2,2)
aa(0,0) = "00"
aa(0,1) = "01"
aa(1,0) = "10"
aa(1,1) = "11"
aa(2,1) = "21"
aa(2,2) = "22"
dim i,j
for i=0 to 2
 for j=0 to 2
response.write"aa(" & cstr(i) &","& cstr(j) &")=" &cstr(aa(i,j))
 next
next
%>
搜索更多相关主题的帖子: 二维 
2011-01-22 09:41
lovemole
Rank: 1
等 级:新手上路
帖 子:58
专家分:7
注 册:2007-6-1
得分:7 
<%
dim aa(2,2)
aa(0,0) = "00"
aa(0,1) = "01"
aa(1,0) = "10"
aa(1,1) = "11"
aa(2,1) = "21"
aa(2,2) = "22"
dim i,j
i=0
j=0
for i=0 to 2
for j=0 to 2
response.write"aa(" & cstr(i) &","& cstr(j) &")=" &cstr(aa(i,j))
j=j+1
next j
i=i+1
next i
%>

2011-01-22 13:24
caoweizhong
Rank: 2
等 级:论坛游民
帖 子:19
专家分:65
注 册:2011-1-22
得分:7 
定义一个简单的数组

有两种方法在asp中定义数组,让我们看看每种的例子:

方法一:
MyArray = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec")

方法二:
Dim myArray(2)

myArray(0)="Jan"
myArray(1)="Feb"

动态改变数组的大小

DIM myArray()
REDIM myArray(20) '将数组重新定义为20维

可扩展数组举例:将数组从0扩展到10
Dim MyArray()
for i = 0 to 10
ReDim Preserve MyArray(i) ‘Preserve   保留原来数据
MyArray(i)=...
next

.................................................................................................

有用的数组函数

Ubound(arrayName)函数

Lbound(arrayName)这个函数是返回数组的上标,也就是数组第一个元素的标记。

.................................................................................................

.................................................................................................

向另一个页面传递数组

现在有很多种方法向另一页面传递数组,目前有三种方法:

定义一个又逗号分隔的字符串,然后再下一页中用Split函数重新建立数组。
将数组存储在一个Session变量中,然后在下一个页面中调用。
通过表单的隐含区域来传递数组,他们都是自动用逗号分开,然后再用Split函数重新建立数组。

前两种方法很好,但是都比第三中复杂。在这里我们将只介绍第三种,因为它是最简单最有效的。

1.asp:
<%
dim I
dim myArray(20)

for I=0 to 20
myArray(I)="Item " & I
next
%>
<html>
<body>
<form name="testform" method="post" action="2.asp">
<%
for I=0 to ubound(myArray)
response.write "<input type=hidden name=myArray value='" & myArray(I) & "'>"
next
%>
<p>
<input type="submit">
</form>
</body>
</html>

以上我们做的是在一个表单中用单独的隐含域存储数组中的每个元素,我们再看看下一页:

2.asp
<html>
<body>
<%
dim arrString
dim myArray
dim I

arrString=request("myArray")
myArray = split(arrString,",")

for I=0 to ubound(myArray)
response.write "Item "&I&" = " & myArray(I) & "<br>" & vbCrLf
next
%>
</body>
</html>

 

------------------------------------------------

将一个字符串分割并返回分割结果的数组
Dim MyArray
MyArray = Split(tempcnt,chr(13)&chr(10))
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next

数组排序函数
function..Sort(ary)
KeepChecking = TRUE
Do Until KeepChecking = FALSE
KeepChecking = FALSE
For I = 0 to UBound(ary)
If I = UBound(ary) Then Exit For
If ary(I) > ary(I+1) Then
FirstValue = ary(I)
SecondValue = ary(I+1)
ary(I) = SecondValue
ary(I+1) = FirstValue
KeepChecking = TRUE
End If
Next
Loop
Sort = ary
End function..

数组排序函数应用例子
Dim MyArray
MyArray = Array(1,5,123,12,98)
MyArray = Sort(MyArray)
For I = Lbound(MyArray) to Ubound(MyArray)
Response.Write MyArray(I) & "<br>"
Next

在Application和Session中使用数组
Application.Lock
Application("StoredArray") = MyArray
Application.Unlock

LocalArray = Application("StoredArray")

覆盖Application中的数组
Application.Lock
Application("StoredArray") = LocalArray
Application.Unlock

Session使用方法与Application相同

从数据库中把数据导入数组中
Dim MyArray
取出全部记录
MyArray = RS.GetRows
取出前10项记录
MyArray = RS.GetRows(10)

For row = 0 To UBound(MyArray, 2)
For col = 0 To UBound(MyArray, 1)
Response.Write (col, row) & "<br>"
Next
Next
2011-01-22 18:40
xuehao
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2011-1-18
得分:0 
我是初学者,还是不懂:
aa(0,0) = "00"
aa(0,1) = "01"
aa(1,0) = "10"
aa(1,1) = "11"
aa(2,1) = "21"
aa(2,2) = "22"
这些元素怎么排出来的,道理不懂,希望能详细解释下,还有哪个循环语句
dim i,j
for i=0 to 2
for j=0 to 2
response.write"aa(" & cstr(i) &","& cstr(j) &")=" &cstr(aa(i,j))
next
next
我需要你们帮忙,加我QQ:1192742894
2011-01-23 09:47
hams
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:18
帖 子:912
专家分:3670
注 册:2008-7-30
得分:7 
以下是引用lovemole在2011-1-22 13:24:55的发言:

 
<%
dim aa(2,2)
aa(0,0) = "00"
aa(0,1) = "01"
aa(1,0) = "10"
aa(1,1) = "11"
aa(2,1) = "21"
aa(2,2) = "22"
dim i,j
i=0
j=0
for i=0 to 2
for j=0 to 2
response.write"aa(" & cstr(i) &","& cstr(j) &")=" &cstr(aa(i,j))
j=j+1
next j
i=i+1
next i
%> 自己检查一下
I和J都是循环变量,还需要清零和累加么?

俺不高手,俺也是来学习的。
俺的意见不一定就对,当你不认同时请点忽视按钮。
当走到十字路口不知该如何走时,可在论坛问下路,但你若希望别人能一路把你送到目的地,显然是不现实的,因为别人也有自己要走的路。
2011-01-24 08:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-331433-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.095401 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved