dim i%,s$,isOK as boolean isOK=true s="," & join(b,",") & ","for i=0 to ubound(a) if instr(s,"," & a(i) & ",")=0 then isOK =false exit for end if next msgbox iif(isOK,"b包含a!","b不包含a!")
哦 不好意思 我没说清楚,不是字符串数组啊,是long,是下面这样的 dim a(2) as long dim b(9) as long a=(1,2,3) b=(1,4,5,21,4,3,12,222,44,2)
Private Sub Command1_Click() Dim c As Variant Dim d As Variant Dim a(2) As Long Dim b(9) As Long Dim i As Integer Dim j As Integer Dim blnHave As Boolean
c = Array(1, 2, 3) d = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
For i = 0 To UBound(c) a(i) = c(i) Next i
For i = 0 To UBound(d) b(i) = d(i) Next i
For i = 0 To UBound(a) blnHave = False
For j = 0 To UBound(b) If a(i) = b(j) Then Me.Print a(i) & " 存在, 位置 " & j blnHave = True Exit For End If Next j Next i
End Sub
Private Sub Form_Load() Dim a, b a = Array(1, 2, 3) b = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
Dim i&, j&, isExist As Boolean, isFind As Boolean
isExist = True For i = 0 To UBound(a) isFind = False For j = 0 To UBound(b) If a(i) = b(j) Then Debug.Print j isFind = True Exit For End If Next If Not isFind Then isExist = False Exit For End If Next
MsgBox IIf(isExist, "b包含a!", "b不包含a!") End Sub
Private Sub Command1_Click() Dim c As Variant Dim d As Variant Dim a(2) As Long Dim b(9) As Long Dim i As Integer Dim j As Integer Dim blnHave As Boolean Dim blnFindAll As Boolean
c = Array(1, 2, 3) d = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
For i = 0 To UBound(c) a(i) = c(i) Next i
For i = 0 To UBound(d) b(i) = d(i) Next i
If UBound(a) > 0 And UBound(b) > 0 Then blnFindAll = True Else blnFindAll = False End If
For i = 0 To UBound(a) blnHave = False
For j = 0 To UBound(b) If a(i) = b(j) Then Me.Print a(i) & " 存在, 位置 " & j blnHave = True Exit For End If Next j
If blnHave = False Then blnFindAll = False End If Next i
遍历是最简单的,也是最低效的,当然楼主示例的这么小的数组,似乎效率这层可以忽略了... 说到算法,假设如楼主所示是数据都整数的话,可以用空间换时间,思路: dim a,b dim i as long dim Idx() as long dim maxIdx as long dim tmp()a = Array(1, 2, 3) b = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)'先循环小数组得到一个最大值 for i=1 to ubound(a) if a(i)>a(maxIdx) then maxIdx=i next '按最大值声明一个存取大数组下标的数组 redim Idx(a(maxIdx)) '循环大数数组写入对应值的下标 for i=0 to ubound(b) if b(i)<= a(maxIdx) then if Idx(b(i))=0 then Idx(b(i))=i+1 '如果有重复值,只取第一次出现的下标 end if next '循环小数组,验证 tmp=a for i=0 to ubound(a) if idx(a(i))>0 then tmp(i)=idx(a(i))-1 else msgbox "b 不是包含 a" exit sub end if next msgbox "b 在 a中对应的下标为: "& join(tmp) 如上思路三个循环搞定....
isOK=true
s="," & join(b,",") & ","for i=0 to ubound(a)
if instr(s,"," & a(i) & ",")=0 then
isOK =false
exit for
end if
next
msgbox iif(isOK,"b包含a!","b不包含a!")
dim a(2) as long
dim b(9) as long
a=(1,2,3)
b=(1,4,5,21,4,3,12,222,44,2)
最多instr的时候cstr(a(i))下
1.先把数组B转为List对象.
2.遍历A数组,然后调用List类的contains方法来判断.
请给分哦.呵呵
a=(1,2,3)
b=(1,4,5,21,4,3,12,222,44,2)相对应的下标就是 0 9 5
Dim c As Variant
Dim d As Variant
Dim a(2) As Long
Dim b(9) As Long
Dim i As Integer
Dim j As Integer
Dim blnHave As Boolean
c = Array(1, 2, 3)
d = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
For i = 0 To UBound(c)
a(i) = c(i)
Next i
For i = 0 To UBound(d)
b(i) = d(i)
Next i
For i = 0 To UBound(a)
blnHave = False
For j = 0 To UBound(b)
If a(i) = b(j) Then
Me.Print a(i) & " 存在, 位置 " & j
blnHave = True
Exit For
End If
Next j
Next i
End Sub
Dim a, b
a = Array(1, 2, 3)
b = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
Dim i&, j&, isExist As Boolean, isFind As Boolean
isExist = True
For i = 0 To UBound(a)
isFind = False
For j = 0 To UBound(b)
If a(i) = b(j) Then
Debug.Print j
isFind = True
Exit For
End If
Next
If Not isFind Then
isExist = False
Exit For
End If
Next
MsgBox IIf(isExist, "b包含a!", "b不包含a!")
End Sub
Dim c As Variant
Dim d As Variant
Dim a(2) As Long
Dim b(9) As Long
Dim i As Integer
Dim j As Integer
Dim blnHave As Boolean
Dim blnFindAll As Boolean
c = Array(1, 2, 3)
d = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)
For i = 0 To UBound(c)
a(i) = c(i)
Next i
For i = 0 To UBound(d)
b(i) = d(i)
Next i
If UBound(a) > 0 And UBound(b) > 0 Then
blnFindAll = True
Else
blnFindAll = False
End If
For i = 0 To UBound(a)
blnHave = False
For j = 0 To UBound(b)
If a(i) = b(j) Then
Me.Print a(i) & " 存在, 位置 " & j
blnHave = True
Exit For
End If
Next j
If blnHave = False Then
blnFindAll = False
End If
Next i
Me.Print IIf(lbnfindall, "", "没有") & "全部找到"
End Sub
说到算法,假设如楼主所示是数据都整数的话,可以用空间换时间,思路:
dim a,b
dim i as long
dim Idx() as long
dim maxIdx as long
dim tmp()a = Array(1, 2, 3)
b = Array(1, 4, 5, 21, 4, 3, 12, 222, 44, 2)'先循环小数组得到一个最大值
for i=1 to ubound(a)
if a(i)>a(maxIdx) then maxIdx=i
next
'按最大值声明一个存取大数组下标的数组
redim Idx(a(maxIdx))
'循环大数数组写入对应值的下标
for i=0 to ubound(b)
if b(i)<= a(maxIdx) then
if Idx(b(i))=0 then Idx(b(i))=i+1 '如果有重复值,只取第一次出现的下标
end if
next
'循环小数组,验证
tmp=a
for i=0 to ubound(a)
if idx(a(i))>0 then
tmp(i)=idx(a(i))-1
else
msgbox "b 不是包含 a"
exit sub
end if
next
msgbox "b 在 a中对应的下标为: "& join(tmp) 如上思路三个循环搞定....